更新时间:2023-11-26 09:44:52
创建JSONObjects a nd JSONArrays手工操作可能非常麻烦,下面我将向您展示如何使用Gson库创建并显示为json数据结构:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class Stats
{
private String metric;
私人长时间戳;
private int value;
私人地图< String,String> tags = new HashMap<>();
/ **
* @返回指标
* /
public String getMetric(){
return metric;
}
/ **
* @param度量标准设置
* /
public void setMetric(String metric){
this.metric =度量;
$ b $ **
* @返回时间戳
* /
public long getTimestamp(){
return timestamp;
}
/ **
* @param timestamp设置
* /
的时间戳public void setTimestamp(long timestamp){
this.timestamp =时间戳;
}
/ **
* @返回值
* /
public int getValue(){
返回值;
}
/ **
* @param value设置的值
* /
public void setValue(int value){
this.value =值;
}
/ **
* @return the tags
* /
public Map< String,String> getTags(){
返回标签;
}
/ **
* @param标记标签以设置
* /
public void setTags(Map< String,String> tags){
this.tags =标签;
$ b public class JSON {
public static final main(String args [])
{
列表与LT;统计> stats = new ArrayList< Stats>();
//填写数据,你知道,无论
Stats stat1 = new Stats();
stat1.setMetric(metric1);
stat1.getTags()。put(tag1,value1);
stat1.getTags()。put(tag2,value2);
统计信息stat2 = new Stats();
stat2.setMetric(metric2);
// ...填充数据...
// ...将数据添加到数组
stats.add(stat1);
stats.add(stat2);
Gson gson = new GsonBuilder()。setPrettyPrinting()。create();
System.out.println(gson.toJson(stats));
返回:
$ $ $
metric:metric1,
timestamp:0,
value :0,
tags:{
tag2:value2,
tag1:value1
}
},
{
metric:metric2,
timestamp:0,
value:0,
tags:{}
}
]
I am trying to add multiple data in single JSON object but its getting overwritten.
I looked at some of the *** 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?
Creating JSONObjects and JSONArrays by hand can be really messy, here I show you how to create and show as json your data structure by using Gson library:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
class Stats
{
private String metric;
private long timestamp;
private int value;
private Map<String,String> tags=new HashMap<>();
/**
* @return the metric
*/
public String getMetric() {
return metric;
}
/**
* @param metric the metric to set
*/
public void setMetric(String metric) {
this.metric = metric;
}
/**
* @return the timestamp
*/
public long getTimestamp() {
return timestamp;
}
/**
* @param timestamp the timestamp to set
*/
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
/**
* @return the value
*/
public int getValue() {
return value;
}
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}
/**
* @return the tags
*/
public Map<String, String> getTags() {
return tags;
}
/**
* @param tags the tags to set
*/
public void setTags(Map<String, String> tags) {
this.tags = tags;
}
}
public class JSON {
public static final void main(String args[])
{
List<Stats> stats=new ArrayList<Stats>();
// Fill data, you know, whatever
Stats stat1=new Stats();
stat1.setMetric("metric1");
stat1.getTags().put("tag1","value1");
stat1.getTags().put("tag2","value2");
Stats stat2=new Stats();
stat2.setMetric("metric2");
// ... Fill data...
// ... Add stats to array
stats.add(stat1);
stats.add(stat2);
Gson gson=new GsonBuilder().setPrettyPrinting().create();
System.out.println(gson.toJson(stats));
}
}
Returning:
[
{
"metric": "metric1",
"timestamp": 0,
"value": 0,
"tags": {
"tag2": "value2",
"tag1": "value1"
}
},
{
"metric": "metric2",
"timestamp": 0,
"value": 0,
"tags": {}
}
]