在Android中,从带有嵌套json对象的json和带有多个json对象的嵌套json数组中获取字符串
我需要以字符串形式访问复杂Json中包含的所有单个参数 例如在Android中,从带有嵌套json对象的json和带有多个json对象的嵌套json数组中获取字符串,android,json,Android,Json,我需要以字符串形式访问复杂Json中包含的所有单个参数 例如stringpeople=。。。; 字符串idPeople=等 我曾尝试使用JSONTokeners,因为我曾尝试搜索类似的问题,对于简单的json,我没有问题,但我不知道如何从中正确获取参数: {"id":1,"error":null,"result": {"nPeople":2, "people":[ {"namePeople":"Inca", "power":"1235
stringpeople=。。。;
字符串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个或更多的people对象如果我们调用您发布的json myJsonString
JSonObject obj = new JSonObject(myJsonString);
JSonObject result = obj.getJSONObject("result");
JSonArray people = result.getJSONArray("people");
int numOfPeople = result.getInt("nPeople");
我没有试过。
但我想它可能会起作用
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");
...
}
JSONObject obj=新的JSONObject(jsonString);
字符串id=obj.getString(“id”);
字符串错误=obj.getString(“错误”);
JSONObject result=obj.getJSONObject(“结果”);
int nPeople=result.getInt(“nPeople”);
JSONArray people=result.getJSONArray(“people”);
for(int i=0;i
这让我快疯了。在objective-c之后,我涉猎了Android开发,但没有弄明白这一点。谢谢在这个答案中有更多问题的答案。塔克斯