0

I have this JSON for example:

{
"mesaje": [{
    "cif": "111",
    "data_creare": "29.11.2019 07:52:24",
    "id_solicitare": "222",
    "tip": "SOLICITARE",
    "id": "333",
    "detalii": "duplicat  pentru CUI 111"
}, {
    "cif": "444",
    "data_creare": "29.11.2019 07:59:37",
    "id_solicitare": "555",
    "tip": "SOLICITARE",
    "id": "666",
    "detalii": "duplicat pentru CUI 888"
}],
  "serial": "aaaaaaaaaaaaaaaaa",
  "cnp": "1888888888888888"

This is the code that I have written in Java:

    public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {


        JSONParser parser = new JSONParser();

        try (Reader reader = new FileReader("D:\\test.json")) {
            JSONObject jsonObject = (JSONObject) parser.parse(reader);
            System.out.println(jsonObject);

            String cif = (String) jsonObject.get("serial");
            System.out.println(cif);
        }

    }

This prints out the serial number...but what I would like to do is to acces for example in mesaje -> cif , or. mesaje -> data_creare....the library used is json-simple...can you please help me with how I could do that ? Thanks in advance

1
  • JSON in your question i snot a valid json Please update with a valid json. Commented Jan 28, 2020 at 7:16

2 Answers 2

2

You can read the documentation of json-simple and check various methods/examples to read/travserse the json at json-simple.

Example to read mesaje -> cif.

JSONObject jsonObject = (JSONObject) parser.parse(reader);
JSONArray mesaje = (JSONArray) jsonObject.get("mesaje");
Iterator<String> iterator = mesaje.iterator();
while (iterator.hasNext()) {
    JSONObject mesajeItem = iterator.next();
    System.out.println((String) mesajeItem.get("cif"));
}

More examples available at json-simple examples

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

Comments

-1

You can try this below code.

 JSONArray jsonArray = (JSONArray) jsonObject.get("mesaje");
  for (int i = 0; i <jsonArray.length(); i++) {
  JSONObject obj= jsonArray.get(i);
  String cifId=obj.get("cif");
  String data_creare=obj.get("data_creare");
  String str1=obj.get("id_solicitare");
  String str2=obj.get("tip");
  String str3=obj.get("id");
  String str4=obj.get("detalii");
  String description=obj.get("description");
  System.out.println("Print here);        
}
 System.out.println("Print result here");

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.