0

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

JSONObject json = (JSONObject)parser.parse(responseBody);
        JSONArray values = (JSONArray) json.get("value");
        for(Object o: values){
            if ( o instanceof JSONObject ) {
                String projectName = (String) ((JSONObject)o).get("name");
                System.out.println(projectName);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.