在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",
             "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}]
    }
}

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");
        ...
    }

转载注明原文:在Android中,使用嵌套的json对象和带有多个json对象的嵌套json数组从json获取String - 代码日志