Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transform list in a JSON, then adding an header on top of this JSON

I have a list of objects:

List<ScuolaEntity> result = scuolaService.getAllScuoleEntity();

I would like to convert the list in a json object, I have tried with gson, with JSONObject and even with JsonNode, but I couldn’t find a way to add a header/node to the json.

This is the actual json:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
    {
        "codiceMeccanografico": "RMAT123456",
        "codiceFiscale": "CREMHL90T42B745CQQE",
        "denominazione": "ITS INCOM"
    },
    {
        "codiceMeccanografico": "RMAT891011",
        "codiceFiscale": "SCEMQL90T42B735CWQ1",
        "denominazione": "Liceo Scientifico Statale E. Lussu"
    }
]

This is what I would like to achieve

{
  "scuole": [
    {
        "codiceMeccanografico": "RMAT123456",
        "codiceFiscale": "CREMHL90T42B745CQQE",
        "denominazione": "ITS INCOM"
    },
    {
        "codiceMeccanografico": "RMAT891011",
        "codiceFiscale": "SCEMQL90T42B735CWQ1",
        "denominazione": "Liceo Scientifico Statale E. Lussu"
    }
  ]
}

Can I add this header "scuole" with gson library or another?

>Solution :

you can achieve this by wrapping this list into a map:

List<ScuolaEntity> result = scuolaService.getAllScuoleEntity();
Map data=Collections.singletoneMap("scuole",result);

And then encode this map as JSON.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading