Friday, February 28, 2014

How does this json data parsing using Gson? Urgent!


I wrote the first to ask this json data format, right? Second, how to resolve it? Asked to use Gson resolution.
 
{

"Status": "success",

"Data": {

[{"Id": 1000, "name": "zhangsan", "phones": ["15611111111", "15622222222"]},

{"Id": 1001, "name": "lisi", "phones": ["15611111111", "15622222222"]},

{"Id": 1002, "name": "wangwu", "phones": ["15611111111", "15622222222"]}

]

}

}

User data is a number of objects.
 
public class User {

private int id;

private String name;

private List phones;

public int getId () {

return id;

}

public void setId (int id) {

this.id = id;

}

public String getName () {

return name;

}

public void setName (String name) {

this.name = name;

}

public List getPhones () {

return phones;

}

public void setPhones (List phones) {

this.phones = phones;

}

}

Wherein the telephone may also be plural.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
That some map ah, layer by layer Flayer wants.
Reply:
cited a floor reply:
that some map ah, layer by layer Flayer wants.

gson good package to resolve an array of objects and methods, the feeling is very convenient. But I json object above a slightly more complex, there is an object, another array, as well as nesting. Want to use a combination of objects and parsing gson array of analytical methods to solve. May I ask how to achieve it? I know there are a JsonReader gson can be resolved, just feel good trouble!
Reply:
Want to know whether you can verify
json right herehttp://www.bejson.com/go.html?u=http://www.bejson.com/index.php

After I find you to verify this is not a standard json format
Your modified to
{
"Status": "success",
"Data": [
{
"Id": 1000,
"Name": "zhangsan",
"Phones": [
"15611111111",
"15622222222"
]
},
{
"Id": 1001,
"Name": "lisi",
"Phones": [
"15611111111",
"15622222222"
]
},
{
"Id": 1002,
"Name": "wangwu",
"Phones": [
"15611111111",
"15622222222"
]
}
]
}

http://www.json.org/ some json syntax
The target date of symbols {} removed, if it is an object, you will need to give a key

As for parsing, install your format one by one, on the use of an object is the object function is an array on an array of functions.


Reply:
reference to the third floor of the reply:
json want to know whether you can properly verify here
http://www.bejson.com/go.html?u=http://www.bejson.com/index.php

After I find you to verify this is not a standard json format
Your modified to
{
"Status": "success",
"Data": [
{
"Id": 1000,
"Name": "zhangs ......

Thank you very much for your answer! I forgive ignorance, I want to ask next: If you are using org.json package, fully able to get, such as the use gson, I was able to resolve the object, but also to resolve the array. But I want to use a combination of these two methods to resolve the above example is not to resolve life and death. If gson of JsonReader of beginX, endX way I can get. Just feel stupid ・ ・ ・ especially numbers which have more than one number, his head has not turned the corner, but also look heroes do not laugh! Here is my method:
 
public class ResultInfo {
private String status;
private String data;
public String getStatus () {
return status;
}
public void setStatus (String status) {
this.status = status;
}
public String getData () {
return data;
}
public void setData (String data) {
this.data = data;
}
}
public class JsonParser {
public static ResultInfo parseResultInfo (String jsonData) {
ResultInfo info = null;
if (jsonData! = null) {
Gson gson = new Gson ();
info = gson.fromJson (jsonData, ResultInfo.class);
}
return info;
}
public static List parseUser (String jsonData) {
ResultInfo info = parseResultInfo (jsonData);
if (info.getStatus (). equals ("success")) {
Type listType = new TypeToken > () {} getType ();.
Gson gson = new Gson ();
List list = gson.fromJson (info.getData (), listType);
return list;
}
return null;
}
}




Reply:
Brothers, there is no trick up? I also think of ways to die. : (
Reply:
I do not understand you with such a complex json gson how to resolve? The landlord did not get? Seek advice ah
Reply:
jsonobject, jsonarray, nesting only, {} is jsonobject, [] is jsonarray, little by little, it wants out solutions based on key
Reply:
references, 5th Floor reply:
brothers have recruited? I also think of ways to die. : (

Wood, Gaolebantian not get out, and finally use the tools in the package within org.json resolved. Also very simple. Brother told me the trouble of finding out under the next.
Reply:
It seems a good resolve
Reply:
Json format looks like a problem! ! !

No comments:

Post a Comment