I am trying to add multiple data in single JSON object but its getting overwritten.
I looked at some of the stackoverflow question but i couldn't find any answers .(maybe i dont know how to search in google)
Gson turn an array of data objects into json - Android
This is what i have done so far:-
This is my code using GSON library :-
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("metric", "name1");
jsonObject.addProperty("timestamp", 1443785014);
jsonObject.addProperty("value", 18);
JsonObject jObject = new JsonObject();
jObject.addProperty("host", "one");
jObject.addProperty("host1", "two");
jsonObject.add("tags",jObject);
jsonObject.addProperty("metric", "name2");
jsonObject.addProperty("timestamp", 1443785014);
jsonObject.addProperty("value", 9);
jObject.addProperty("host", "one");
jObject.addProperty("host1", "two");
jsonObject.add("tags",jObject);
System.out.println(jsonObject);
This is what i get as output :-
{
"metric": "name2",
"timestamp": 1346846400,
"value": 9,
"tags": {
"host": "one",
"host1": "two"
}
}
here is what i want as output :-
[
{
"metric": "name1",
"timestamp": 1346846400,
"value": 18,
"tags": {
"host": "one",
"host1": "two"
}
},
{
"metric": "name2",
"timestamp": 1346846400,
"value": 9,
"tags": {
"host": "one",
"host1": "two"
}
}
]
Why i am not getting both name1
and name2
in JSON Object?