Get String from json with nested json object and nested json arrays with multiple json object, in Android

后端 未结 2 884
伪装坚强ぢ
伪装坚强ぢ 2021-02-04 19:10

I need to access as String all the single parameters contained in a complex Json.

for example String people=...; String idPeople=...; etc.

相关标签:
2条回答
  • 2021-02-04 19:38

    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");
            ...
        }
    
    0 讨论(0)
  • 2021-02-04 19:43

    if we call the json you post myJsonString,

    JSonObject obj = new JSonObject(myJsonString);
    JSonObject result = obj.getJSONObject("result");
    JSonArray people = result.getJSONArray("people");
    int numOfPeople = result.getInt("nPeople");
    
    0 讨论(0)
提交回复
热议问题