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

JAVA,GSON:-使用GSON在json对象中添加多个数据,java,json,gson,Java,Json,Gson,我试图在一个JSON对象中添加多个数据,但它会被覆盖 我看了一些stackoverflow问题,但找不到任何答案。(也许我不知道如何在谷歌上搜索) 这就是我到目前为止所做的:- 这是我使用GSON库的代码:- JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("metric", "name1"); jsonObject.addProperty("timestamp", 1443785014); jsonObje

我试图在一个JSON对象中添加多个数据,但它会被覆盖

我看了一些stackoverflow问题,但找不到任何答案。(也许我不知道如何在谷歌上搜索)

这就是我到目前为止所做的:-

这是我使用GSON库的代码:-

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);
这是我得到的输出:-

{
    "metric": "name2",
    "timestamp": 1346846400,
    "value": 9,
    "tags": {
       "host": "one",
       "host1": "two"
    }
}
[
    {
        "metric": "name1",
        "timestamp": 1346846400,
        "value": 18,
        "tags": {
           "host": "one",
           "host1": "two"
        }
    },
    {
        "metric": "name2",
        "timestamp": 1346846400,
        "value": 9,
        "tags": {
           "host": "one",
           "host1": "two"
        }
    }
]
以下是我想要的输出:-

{
    "metric": "name2",
    "timestamp": 1346846400,
    "value": 9,
    "tags": {
       "host": "one",
       "host1": "two"
    }
}
[
    {
        "metric": "name1",
        "timestamp": 1346846400,
        "value": 18,
        "tags": {
           "host": "one",
           "host1": "two"
        }
    },
    {
        "metric": "name2",
        "timestamp": 1346846400,
        "value": 9,
        "tags": {
           "host": "one",
           "host1": "two"
        }
    }
]

为什么我没有在JSON对象中同时获得
name1
name2

您正在写入同一个对象并序列化它,这就是它被覆盖的原因
JsonObject
只是一个映射,写入同一个键将覆盖现有值


创建一个
JsonArray
和两个新的
JsonObject
对象。将每一个添加到
JsonArray

手动创建JSONObjects和JsonArray可能会非常混乱,下面我将向您展示如何使用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 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));
    }
}