Java,GSON :- add multiple data in json object using GSON

标签 java json gson

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

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

Gson turn an array of data objects into 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

最佳答案

手动创建 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": {}
  }
]

关于Java,GSON :- add multiple data in json object using GSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906634/

相关文章:

c# - 字节字符串到字节数组c#

objective-c - Objective-C 中的 Gson 等价物

java - 防止Gson转义Unicode符号

java - 在 JTable 链接模型上同步删除元素以避免 NullPointerException?

java - 如何在 JavaFX 中获得窄进度条?

java - 不同颜色值的JTable

iphone - 递归遍历未知结构的NSDictionary

java - java中有没有像setw()这样的内置函数?如果不是,我该如何格式化它?

wcf - 使用 jQuery 访问 JSON WCF 服务时未找到 404

android - 将 Gson 转换为对象?