我需要以字符串形式访问复杂Json中包含的所有单个参数。
例如String people=...;
String idPeople=...;
等。
我曾尝试使用JSONTokeners,因为我试图搜索类似的问题,而对于简单的json,我没有问题,但我不知道如何从中正确获取参数:
{"id":1,"error":null,"result":
{"nPeople":2,
"people":[
{"namePeople":"Inca",
"power":"1235",
"location":"asdfghjja",
"idPeople":189,
"mainItems":"brownGem",
"verified":false,
"description":"Lorem impsum bla bla",
"linkAvatar":"avatar_12.jpg",
"longitude":16.2434263,
"latitude":89.355118},
{"namePeople":"Maya",
"power":"1235",
"location":"hcjkjhljhl",
"idPeople":119,
"mainItems":"greenstone",
"verified":false,
"description":"Lorem impsum bla bla",
"linkAvatar":"avatar_6.jpg",
"longitude":16.2434263,
"latitude":89.3551185}]
}
}
注意数组中对象的数量并不总是2 ...并且可能包含4个或更多人对象
答案 0 :(得分:20)
我没试过。 但我想它可能有用。
JSONObject obj = new JSONObject(jsonString);
String id = obj.getString("id");
String error = obj.getString("error");
JSONObject result = obj.getJSONObject("result");
int nPeople = result.getInt("nPeople");
JSONArray people = result.getJSONArray("people");
for(int i = 0 ; i < people.length() ; i++){
JSONObject p = (JSONObject)people.get(i);
String namePeople = p.getString("namePeople");
...
}
答案 1 :(得分:2)
如果我们调用json你发布myJsonString,
JSonObject obj = new JSonObject(myJsonString);
JSonObject result = obj.getJSONObject("result");
JSonArray people = result.getJSONArray("people");
int numOfPeople = result.getInt("nPeople");