2014-11-03 8 views
0

Я разрабатываю приложение, которое использует много json, я использовал Gson для разбора, но это было очень медленно, и теперь я пытаюсь использовать Джексона, это первый случай, когда Я слышал об этом, поэтому не знаю, как использовать его очень хорошо. Мои JS подмигнули как:Parse Json с Jackson Android

{ 
    "action": "login", 
    "status": true, 
    "message": "OK", 
    "duration": 144, 
    "response": { 
     "token": "b6a1e3b87c86531d91afa91bb23aca46" 
    } 
} 

И мой класс десериализация подмигнула нравится:

public class HttpWebServiceObject { 
    private String action; 
    private boolean status; 
    private String message; 
    private String duration; 
    private String response; 

    //getters and setters 

} 

Кто-то может помочь мне с этим?

Заранее спасибо.

Редакция:

я могу иметь JSON как это тоже:

{ 
    "action": "getUserFollowers", 
    "status": true, 
    "message": "OK", 
    "duration": 187, 
    "response": [ 
     { 
      "id": "20810", 
      "name": "thyago", 
      "username": "thyago", 
      "location": "guarujá,brasil", 
      "photo": "profile/48d15179063126b40f6a5b1bbdea7f1e.jpg", 
      "following": false, 
      "numfollowing": 3, 
      "numfollowers": 7 
     }, 
     { 
      "id": "933", 
      "name": "Edson Alves", 
      "username": "prazermatheus", 
      "location": "Rio de Janeiro, Brasil.", 
      "photo": "profile/bcc90c29054781adcd4569bb59251dba.jpg", 
      "following": false, 
      "numfollowing": 2689, 
      "numfollowers": 1373 
     }, 
     { 
      "id": "753", 
      "name": "Yasmim Teófilo", 
      "username": "yasmim_gomesz", 
      "location": "Pacajus, Brasil", 
      "photo": "/profile/default.png", 
      "following": false, 
      "numfollowing": 1, 
      "numfollowers": 6 
     }, 
     { 
      "id": "531", 
      "name": "Muriel Aragão", 
      "username": "muriaragao", 
      "location": "Salvador, Brasil", 
      "photo": "profile/0f6b504736723ea80c9cd15dabb3f0c5.jpg", 
      "following": false, 
      "numfollowing": 348, 
      "numfollowers": 32 
     }, 
     { 
      "id": "492", 
      "name": "shashank", 
      "username": "shashank", 
      "location": "india", 
      "photo": "/profile/default.png", 
      "following": false, 
      "numfollowing": 5, 
      "numfollowers": 3 
     }, 
     { 
      "id": "307", 
      "name": "Clineu Iansen", 
      "username": "clineu", 
      "location": "Curitiba, Brazil", 
      "photo": "profile/3d37a1e9c795a83c672ecacf575ee30a.jpg", 
      "following": false, 
      "numfollowing": 57, 
      "numfollowers": 946 
     }, 
     { 
      "id": "277", 
      "name": "maharshi", 
      "username": "india", 
      "location": "india", 
      "photo": "profile/86d403617a30469f11403f0c9c65141b.jpg", 
      "following": true, 
      "numfollowing": 16, 
      "numfollowers": 1848 
     }, 
     { 
      "id": "57", 
      "name": "Alexandre", 
      "username": "xandyhsf", 
      "location": "Itajai, Brasil", 
      "photo": "/profile/default.png", 
      "following": false, 
      "numfollowing": 51, 
      "numfollowers": 16 
     }, 
     { 
      "id": "34", 
      "name": "Karina Ceconello Ton", 
      "username": "KarinaCeconello", 
      "location": "Quatro Barras/Brasil", 
      "photo": "profile/9bc1fb97263dc9a242766c87e4acf1c4.jpg", 
      "following": false, 
      "numfollowing": 5, 
      "numfollowers": 198 
     }, 
     { 
      "id": "25", 
      "name": "Thiago Bodruk", 
      "username": "ThiagoBodruk", 
      "location": "Quatro Barras, Brazil", 
      "photo": "profile/56111881a4ebe0f0fc5cb85faa262e17.jpg", 
      "following": false, 
      "numfollowing": 26, 
      "numfollowers": 221 
     } 
    ] 
} 

Как я мог бы сделать общий парсер для deseriliaze это и просто принять ответ в виде строки?

+0

Учитывая размер вашего json, gson не должен быть медленным. – njzk2

+0

В этом запросе, gson это не медленно, а в другом да. – jackcar

ответ

0

первые изменения вашей POJO как этот

public class HttpWebServiceObject { 
    private String action; 
    private boolean status; 
    private String message; 
    private String duration; 
    private Response response; // as your json string it is a object. not a String. so create a seperate pojo for response 

    //getters and setters 

}

, то вы можете использовать ObjectMapper для преобразования объектов ява из JSon строки. , например

ObjectMapper mapper = new ObjectMapper(); 
HttpWebServiceObject httpWebServiceObject = mapper.readValue(yourJsonString,HttpWebServiceObject.class); 
0

Так это то, что ваш HttpWebServiceObject.java и Response.java должен выглядеть. Это было сгенерировано с использованием this website, который принимает json и преобразует его в pojos. После того, как у вас есть эти два класса, вы можете использовать ObjectMapper для сопоставления строки json этим классам.

----------------------------------- com.example.HttpWebServiceObject.java ----- -------------------------------

package com.example; 

import java.util.HashMap; 
import java.util.Map; 
import javax.annotation.Generated; 
import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 

@JsonInclude(JsonInclude.Include.NON_NULL) 
@Generated("org.jsonschema2pojo") 
@JsonPropertyOrder({ 
"action", 
"status", 
"message", 
"duration", 
"response" 
}) 
public class HttpWebServiceObject { 

@JsonProperty("action") 
private String action; 
@JsonProperty("status") 
private Boolean status; 
@JsonProperty("message") 
private String message; 
@JsonProperty("duration") 
private Integer duration; 
@JsonProperty("response") 
private Response response; 
@JsonIgnore 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

/** 
* 
* @return 
* The action 
*/ 
@JsonProperty("action") 
public String getAction() { 
return action; 
} 

/** 
* 
* @param action 
* The action 
*/ 
@JsonProperty("action") 
public void setAction(String action) { 
this.action = action; 
} 

/** 
* 
* @return 
* The status 
*/ 
@JsonProperty("status") 
public Boolean getStatus() { 
return status; 
} 

/** 
* 
* @param status 
* The status 
*/ 
@JsonProperty("status") 
public void setStatus(Boolean status) { 
this.status = status; 
} 

/** 
* 
* @return 
* The message 
*/ 
@JsonProperty("message") 
public String getMessage() { 
return message; 
} 

/** 
* 
* @param message 
* The message 
*/ 
@JsonProperty("message") 
public void setMessage(String message) { 
this.message = message; 
} 

/** 
* 
* @return 
* The duration 
*/ 
@JsonProperty("duration") 
public Integer getDuration() { 
return duration; 
} 

/** 
* 
* @param duration 
* The duration 
*/ 
@JsonProperty("duration") 
public void setDuration(Integer duration) { 
this.duration = duration; 
} 

/** 
* 
* @return 
* The response 
*/ 
@JsonProperty("response") 
public Response getResponse() { 
return response; 
} 

/** 
* 
* @param response 
* The response 
*/ 
@JsonProperty("response") 
public void setResponse(Response response) { 
this.response = response; 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperty(String name, Object value) { 
this.additionalProperties.put(name, value); 
} 

} 

------------ ----------------------- com.example.Response.java -------------------- ---------------

package com.example; 

import java.util.HashMap; 
import java.util.Map; 
import javax.annotation.Generated; 
import com.fasterxml.jackson.annotation.JsonAnyGetter; 
import com.fasterxml.jackson.annotation.JsonAnySetter; 
import com.fasterxml.jackson.annotation.JsonIgnore; 
import com.fasterxml.jackson.annotation.JsonInclude; 
import com.fasterxml.jackson.annotation.JsonProperty; 
import com.fasterxml.jackson.annotation.JsonPropertyOrder; 

@JsonInclude(JsonInclude.Include.NON_NULL) 
@Generated("org.jsonschema2pojo") 
@JsonPropertyOrder({ 
"token" 
}) 
public class Response { 

@JsonProperty("token") 
private String token; 
@JsonIgnore 
private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

/** 
* 
* @return 
* The token 
*/ 
@JsonProperty("token") 
public String getToken() { 
return token; 
} 

/** 
* 
* @param token 
* The token 
*/ 
@JsonProperty("token") 
public void setToken(String token) { 
this.token = token; 
} 

@JsonAnyGetter 
public Map<String, Object> getAdditionalProperties() { 
return this.additionalProperties; 
} 

@JsonAnySetter 
public void setAdditionalProperty(String name, Object value) { 
this.additionalProperties.put(name, value); 
} 

}