How to define a data structure in Json, with the name of the object at the top of the structure?

Viewed 84

I'm working on json and I really need to get this structure:

{

"Identidade":

       [ 

           {  "numero": 1704,  "numeroFinal": 1804,  "id": 28 }, 
           {  "numero": 1806,  "numeroFinal": 1905,  "id": 28 }, 
           {  "numero": 1705,  "numeroFinal": 1706,  "id": 29 }, 
           {  "numero": 1707,  "numeroFinal": 1807,  "id": 30 }

       ]

}

But until now I can only get this one, and I still have to be able to write the ** Identidade** at the top

   [ 

       {  "numero": 1704,  "numeroFinal": 1804,  "id": 28 }, 
       {  "numero": 1806,  "numeroFinal": 1905,  "id": 28 }, 
       {  "numero": 1705,  "numeroFinal": 1706,  "id": 29 }, 
       {  "numero": 1707,  "numeroFinal": 1807,  "id": 30 }

   ]

The code that follows is my current implementation.

public void writeJsonStream(String file, List<Identidade> iden) throws IOException {

    JsonWriter writer = new JsonWriter(new OutputStreamWriter(m_Context.openFileOutput(file, Context.MODE_PRIVATE)));
    writer.setIndent(" ");
    writeArray(writer, util);
    writer.close();
}


public void writeArray(JsonWriter writer, List<Identidade> iden) throws IOException {
    writer.beginArray();
    for (Identidade i : iden) {
        writeIdentidade(writer, i);
    }
    writer.endArray();
}

public void writeIdentidade(JsonWriter writer, Identidade iden) throws IOException 

    writer.beginObject();
    writer.name("numero").value(iden.getM_numero());
    writer.name("numeroFinal").value(iden.numeroFinal());
    writer.name("id").value(iden.getID());
    writer.endObject();
}

Can someone give me a hint on how to add ** Identidade**?

4 Answers

You can try this which uses ObjectMapper of Jackson API.

1) Create a DTO class :

package com.multithreading.concurrency;

public class Identidade {

 private int numero;
 private int numeroFinal;
 private int id;

public Identidade(int numero, int numeroFinal, int id) {
    this.numero = numero;
    this.numeroFinal = numeroFinal;
    this.id = id;
}

// getters and setters
}

2) Then write a parent bean, which will contain the above DTO reference :

public class ParentIdentidade { 

    @JsonProperty("Identidade")
    private List<Identidade> identidade;
    // getters and setters
}

3) Now, write the test class to produce the desired output :

public class TestIdentidade {

public static void main(String[] args) {        

    List<Identidade> idenList = new ArrayList<Identidade>();
    idenList.add(new Identidade(1704, 1804, 28));
    idenList.add(new Identidade(1806, 1905, 28));
    idenList.add(new Identidade(1705, 1706, 29));
    idenList.add(new Identidade(1707, 1807, 30));

    ParentIdentidade parentIden = new ParentIdentidade();
    parentIden.setIdentidade(idenList);

    ObjectMapper mapper = new ObjectMapper();
    try {
        String json = mapper.writeValueAsString(parentIden);
        System.out.println(json);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
  }
}

It is producing the desired output :

{
"identidade": [{
        "numero": 1704,
        "numeroFinal": 1804,
        "id": 28
    }, {
        "numero": 1806,
        "numeroFinal": 1905,
        "id": 28
    }, {
        "numero": 1705,
        "numeroFinal": 1706,
        "id": 29
    }, {
        "numero": 1707,
        "numeroFinal": 1807,
        "id": 30
    }
  ]
}
Related