gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:46:20 25 4
gpt4 key购买 nike

我试图在单个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com