Parsing JSON Array within JSON Object

后端 未结 5 1654
梦毁少年i
梦毁少年i 2020-11-27 14:45

I have some JSON with the following structure:

{\"source\":[
           {\"name\":\"john\",\"age\":20},
           {\"name\":\"michael\",\"age\":25},
                


        
5条回答
  •  一个人的身影
    2020-11-27 15:23

    private static String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
        }
        return sb.toString();
      }
    
     String jsonText = readAll(inputofyourjsonstream);
     JSONObject json = new JSONObject(jsonText);
     JSONArray arr = json.getJSONArray("sources");
    

    Your arr would looks like: [ { "id":1001, "name":"jhon" }, { "id":1002, "name":"jhon" } ] You can use:

    arr.getJSONObject(index)
    

    to get the objects inside of the array.

提交回复
热议问题