问题
i'm having an issue converting Json to List<>, I have tried different solutions but no clue
My json result looks like :
{
"Return":0,
"List":[
{
"Code":524288,
"Label":"TEST"
},
{
"Code":524289,
"Label":"TEST1"
},
{
"Code":524290,
"Label":"TEST2"
}
]
}
My code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Questionaire> qstlist = new ArrayList<Questionaire>();
try {
result = new RequestTask().execute("http:/dd.com/categories/current?owner=ccc").get();
json = new JSONObject(result);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
GsonBuilder gsonb = new GsonBuilder();
Gson gson = gsonb.create();
Type listType = new TypeToken<List<Questionaire>>(){}.getType();
qstlist = (List<Questionaire>) gson.fromJson(result, listType);
Questionaire qst = null;
qst = gson.fromJson(result, Questionaire.class);
Toast.makeText(MainActivity.this, "Result "+qstlist.size(), Toast.LENGTH_SHORT).show();
}
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
My questionnaire class :
public class Questionaire {
String Code;
String Label;
public String getCode() {
return Code;
}
public void setCode(String Code) {
this.Code = Code;
}
public String getLabel() {
return Label;
}
public void setLabel(String Label) {
this.Label = Label;
}
}
Here is everything I can't see what's wrong about that
回答1:
In this JSON, you get a list of {Code, Label} objects, but in the List property of an object.
You first need to encapsulate this list in an other object. As in:
public class QuestionaireList {
public List<Questionaire> List;
}
List<Questionaire> qsts = gson.fromJson(result, QuestionaireList.class).List;
回答2:
You need another class that maps the full result
public class MyJson {
@SerializedName("List")
private List<Questionaire> list;
@SerializedName("Return")
private Integer return1;
//... getters and setters
}
Your type then needs to bind to "MyJson" or whatever you name the class.
回答3:
I'm assuming it's the List part you want to map to 'qstList'. If so then you need to extract it from the rest of the json first:
Gson gson = new Gson();
JsonObject resultObj = gson.fromJson(result, JsonObject.class);
JsonArray jsonList = resultObj.get("List").getAsJsonArray();
Type listType = new TypeToken<List<Questionaire>>(){}.getType();
qstList = gson.fromJson(jsonList, listType);
来源:https://stackoverflow.com/questions/28675452/gson-convert-from-json-to-list