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/

相关文章:

java - 在 apache-karaf 中查看 osgi 修订版的命令

java - 如何运行Akka Grpc Java示例

java - Android X轴加速度计

ios - 在 Swift 的 MailJet 电子邮件中附加 PDF

javascript - 如何在 Javascript 中访问 JSON 结构的子对象?

无需预定义所有内容的 Java JSON 反序列化

java - 发送带有 boolean 值的模型类以改进有时不接受该 boolean 值的端点

java - 使用 ImageJ 在 Java 应用程序中显示 dicom 图像

javascript - Express的req.params格式是什么?

grails - 如何使用grails json View 在同一列表中呈现不同类型的对象?