I am trying to construct a Json Array which contains Json Objects. I remember doing this some time ago with org.json but now i am having trouble doing it with gson. The structure for my json array should be like:
[
{
"colour": "abc",
"time": "xyz
},
{
"colour": "abc",
"time": "xyz"
}
]
i am reading the length and area values from a database by looping through json format files that i got using .get() thanks to jersey.
I have managed to form internal json objects {"colour": "xyz", "time": "abc" }. What i have done is stored these objects in an array of json objects. Then i have tried using gson.toJson method to form the array containing these objects. Although doing so gives me something close to my requirement but i want it to be more compact. Here is the json array that i managed to generate.
[
{
"members": {
"colour": {
"value": "xyz"
},
"time": {
"value": "abc"
}
}
},
{
"members": {
"colour": {
"value": "abc"
},
"time": {
"value": "xyz"
}
}
}
]
I want to get rid of the redundant "members" and "value" keywords.
My code for this json array is
JsonObject[] innerObjJson = new JsonObject[hits];
for (int i=0;i<hits;i++){
String colour = jsonVariable.getAsJsonObject().get(i).getAsJsonObject().get("colour").toString();
String time = jsonVariable.getAsJsonObject().get(i).getAsJsonObject().get("time").toString();
colour = colour.replaceAll("\"" ,"");
time = colour.replaceAll("\"" ,"");
InnerJsonStructure innerObj = new InnerJsonStructure(colour,time);
Gson gson = new Gson();
String innerObjString = gson.toJson(innerObj);
JsonParser parserNew = new JsonParser();
innerObjJson[i] = (JsonObject)parserNew.parse(innerObjString);
}
Gson gson1 = new Gson();
String finalJsonArr = gson1.toJson(innerObjJson);
System.out.println(finalJsonArr);
The InnerJsonStructure is
public class InnerJsonStructure {
public String colour;
public String time;
InnerJsonStructure(String Color, String Time){
this.colour = Color;
this.time = Time;
}
}
Any help?