JAVA,GSON:使用GSON向JSON对象中添加多个数据

3
我想在单个JSON对象中添加多个数据,但它们被覆盖了。
我查看了一些stackoverflow的问题,但是没有找到任何答案。(也许我不知道如何在谷歌上搜索) Gson将数据对象数组转换为json-Android Java-Multiple GSON? 这是我目前所做的:
这是使用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"
        }
    }
]

为什么我在JSON对象中没有获得name1name2两个值?
2个回答

3

手动创建JSONObjects和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 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));
    }
}

返回:

   [
  {
    "metric": "metric1",
    "timestamp": 0,
    "value": 0,
    "tags": {
      "tag2": "value2",
      "tag1": "value1"
    }
  },
  {
    "metric": "metric2",
    "timestamp": 0,
    "value": 0,
    "tags": {}
  }
]

1
您正在写入相同的对象并对其进行序列化,这就是为什么它被覆盖的原因。 JsonObject 只是一个映射,写入相同的键将覆盖现有值。
创建一个 JsonArray 和两个新的 JsonObject 对象。将每个对象添加到 JsonArray 中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接