java gson json,JAVA,GSON: - 使用GSON在json对象中添加多个数据

这篇博客讨论了如何使用Gson库在Java中创建包含多个数据条目的JSON对象。作者遇到了一个问题,即在尝试添加多个数据到同一个JSON对象时,数据被覆盖。通过示例代码和解决方案,博主展示了如何正确构造一个包含多个数据项的JSON数组,并提供了创建JSON对象和数组的正确方法。
摘要由CSDN通过智能技术生成

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)

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 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 getTags() {

return tags;

}

/**

* @param tags the tags to set

*/

public void setTags(Map tags) {

this.tags = tags;

}

}

public class JSON {

public static final void main(String args[])

{

List stats=new ArrayList();

// 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": {}

}

]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值