how to convert JSONArray to List of Object using camel-jackson

后端 未结 5 1781
Happy的楠姐
Happy的楠姐 2020-12-23 09:51

Am having the String of json array as follow

{\"Compemployes\":[
    {
        \"id\":1001,
        \"name\":\"jhon\"
        },
        {
                \"         


        
5条回答
  •  盖世英雄少女心
    2020-12-23 10: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("Compemployes");
    

    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.

提交回复
热议问题