How can I access inner JSON elements with simple json in JAVA?

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

I am not familiar with Java and the JSONParser class but I can at least provide the general concept if you can provide the details.

mesaje is an array. So you must specify which item; in your sample there are two items. So I am only guessing but perhaps jsonObject.get(“mesaje[n].cif”);

The following jsonObject documentation might help.

https://docs.oracle.com/javaee/7/api/javax/json/JsonObject.html

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.