JAVA,GSON: - 使用GSON在json对象中添加多个数据 [英] JAVA,GSON :- add multiple data in json object using GSON

查看:3615
本文介绍了JAVA,GSON: - 使用GSON在json对象中添加多个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

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,
标签:{
host:one,
host1:two
}
}
pre>

这是我想要的结果: -

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





$ b为什么我没有得到 name1 name2 in JSON Object?

解决方案

创建JSONObjects a nd 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 int value;
私人地图< String,String> tags = new HashMap<>();
/ **
* @返回指标
* /
public String getMetric(){
return metric;
}
/ **
* @param度量标准设置
* /
public void setMetric(String metric){
this.metric =度量;
$ b $ **
* @返回时间戳
* /
public long getTimestamp(){
return timestamp;
}
/ **
* @param timestamp设置
* /
的时间戳public void setTimestamp(long timestamp){
this.timestamp =时间戳;
}
/ **
* @返回值
* /
public int getValue(){
返回值;
}
/ **
* @param value设置的值
* /
public void setValue(int value){
this.value =值;
}
/ **
* @return the tags
* /
public Map< String,String> getTags(){
返回标签;
}
/ **
* @param标记标签以设置
* /
public void setTags(Map< String,String> tags){
this.tags =标签;


$ b public class JSON {

public static final main(String args [])
{
列表与LT;统计> stats = new ArrayList< Stats>();
//填写数据,你知道,无论
Stats stat1 = new Stats();
stat1.setMetric(metric1);
stat1.getTags()。put(tag1,value1);
stat1.getTags()。put(tag2,value2);

统计信息stat2 = new Stats();
stat2.setMetric(metric2);
// ...填充数据...

// ...将数据添加到数组
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:{}
}
]


I am trying to add multiple data in single JSON object but its getting overwritten.

I looked at some of the stackoverflow question but i couldn't find any answers .(maybe i dont know how to search in google)

Gson turn an array of data objects into json - Android

Java - Multiple GSON?

This is what i have done so far:-

This is my code using GSON library :-

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);

This is what i get as output :-

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

here is what i want as output :-

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

Why i am not getting both name1 and name2 in JSON Object?

解决方案

Creating JSONObjects and JSONArrays by hand can be really messy, here I show you how to create and show as json your data structure by using Gson library:

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));
    }
}

Returning:

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

这篇关于JAVA,GSON: - 使用GSON在json对象中添加多个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆