Error parsing json with simple-json library

I want to get all the “name” of these Json, I tried iterator and mapString, but without success, the result is always NULL.

try {
                    JSONParser parser = new JSONParser();

                    JSONObject json = (JSONObject) parser.parse(responseBody);
                    System.out.println("Parseamento: " + json);

                String projectName = (String) json.get("name");
                System.out.println("Count: " + projectName);


                } catch (Exception e) {
                    // TODO: handle exception
                }

This is the answer Json

{
   "count":02,
   "value":[
      {
         "visibility":"private",
         "name":"New Business",
         "description":"",
         "id":"",
         "state":"wellFormed",
         "url":"https://dev.azure.com/work/_apis/projects/ac9f8ec3-cb2c-4db9-b600-47e34b217e97",
         "revision":434,
         "lastUpdateTime":"2021-01-08T14:18:01.387Z"
      },
      {
         "visibility":"private",
         "name":"Doctor Strange",
         "id":"d0fa6bda-0699-4c33-a24f-c9dbe9655bdb",
         "state":"wellFormed",
         "url":"https://dev.azure.com/work/_apis/projects/d0fa6bda-0699-4c33-a24f-c9dbe9655bdb",
         "revision":330,
         "lastUpdateTime":"2020-10-01T17:57:23.58Z"
      }
   ]
}

com.googlecode.json-simple json-simple 1.1.1

Answer

The name in your JSON is in the array values.

  1. Get that array:
JSONArray projectArray = (JSONArray) json.get("value");
  1. get the name from each entry of the array:
Object[] projObjArray = projectArray.toArray();
for (int i=0; i<projObjArray.length;  i++) {
    JSONObject project = (JSONObject) projObjArray[i];
    String projectName = (String) project.get("name");
    System.out.println("Count: " + projectName);
}

output:

Count: 2
Count: New Business
Count: Doctor Strange

full code:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Main {

    public static void main(String[] args) {
        String responseBody = "{rn" + "   "count":02,rn" + "   "value":[rn" + "      {rn"
                + "         "visibility":"private",rn" + "         "name":"New Business",rn"
                + "         "description":"",rn" + "         "id":"",rn"
                + "         "state":"wellFormed",rn"
                + "         "url":"https:\/\/dev.azure.com\/work\/_apis\/projects\/ac9f8ec3-cb2c-4db9-b600-47e34b217e97",rn"
                + "         "revision":434,rn" + "         "lastUpdateTime":"2021-01-08T14:18:01.387Z"rn"
                + "      },rn" + "      {rn" + "         "visibility":"private",rn"
                + "         "name":"Doctor Strange",rn"
                + "         "id":"d0fa6bda-0699-4c33-a24f-c9dbe9655bdb",rn"
                + "         "state":"wellFormed",rn"
                + "         "url":"https:\/\/dev.azure.com\/work\/_apis\/projects\/d0fa6bda-0699-4c33-a24f-c9dbe9655bdb",rn"
                + "         "revision":330,rn" + "         "lastUpdateTime":"2020-10-01T17:57:23.58Z"rn"
                + "      }rn" + "   ]rn" + "}";
        try {
            JSONParser parser = new JSONParser();

            JSONObject json = (JSONObject) parser.parse(responseBody);
            System.out.println("Parseamento: " + json);

            JSONArray projectArray = (JSONArray) json.get("value");
            System.out.println("Count: " + projectArray.toArray().length);

            Object[] projObjArray = projectArray.toArray();
            for (int i=0; i<projObjArray.length;  i++) {
                JSONObject project = (JSONObject) projObjArray[i];
                String projectName = (String) project.get("name");
                System.out.println("Count: " + projectName);
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *