在Android中,使用嵌套的json对象和带有多个json对象的嵌套json数组从json获取String

前端之家收集整理的这篇文章主要介绍了在Android中,使用嵌套的json对象和带有多个json对象的嵌套json数组从json获取String前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要以String的形式访问复杂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","location":"hcjkjhljhl","idPeople":119,"mainItems":"greenstone","linkAvatar":"avatar_6.jpg","latitude":89.3551185}]
    }
}

NB数组中对象的数量并不总是2 …并且可能包含4个或更多人对象

解决方法

我没试过.
但我想它可能会奏效.
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");
        ...
    }
原文链接:https://www.f2er.com/android/313809.html

猜你在找的Android相关文章