问题描述
我需要以String的形式访问复杂Json中包含的所有单个参数.
I need to access as String all the single parameters contained in a complex Json.
例如String people=...; String idPeople=...;
等
for example String people=...; String idPeople=...;
etc.
我尝试使用JSONTokeners,就像我尝试搜索类似的问题一样,对于简单的json我没有问题,但是我不知道如何从中正确获取参数:
I have tried to use the JSONTokeners, as I have tried to search for similar question, and for simple json I haven't problem but I don't know how to get the parameters correctly from this:
{"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个或更多的对象
NB the numbers of object in the array people is not always 2... and may contains 4 or more people object
推荐答案
我没有尝试过.但是我想这可能有用.
I've not tried.But i guess it may work.
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");
...
}
这篇关于在Android中从具有嵌套json对象的json获取字符串和具有多个json对象的嵌套json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!