GSON:如何将Object(其本身包含JSON)转换为JSON [英] GSON : How to convert Object (which itself contains JSON) to JSON

查看:744
本文介绍了GSON:如何将Object(其本身包含JSON)转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不同的用例,POJO中的字段本身存储JSON数据,基本上是对所有数据进行分组。



现在我想要从上面的POJO生成完整的JSON。



目前我正在使用这种方法

  private static Gson gson = new Gson(); 
public static String convertObjectToJSON(Object object)throws Exception {
String jsonResponse = gson.toJson(object);
返回jsonResponse;
}

但是输出的转义字符在双引号附近,如

  {\_id \:234242,\name \:\carlos \} 

我尝试了GsonBuilder中的各种选项,但无法正常工作。
基本上,我只是将多个JSON数据分组并发送。



您能否请求帮助摆脱双引号内的转义字符。

更新:



问题是:我有3个JSON,需要将它们合并到一个JSON中需要通过Spring MVC将它传递给html5客户端。截至目前,我将3个JSON添加到POJO中,并试图将其转换为JSON。同上解释。



谢谢, Regards


解决方案

在双引号周围没有任何转义字符。

  class MyPOJO {
private int _id;
私人字符串名称;
// getter& setter
}

String json ={\_id \:234242,\name \:\carlos \};
MyPOJOobj = new Gson()。fromJson(json,MyPOJO.class);
System.out.println(new Gson()。toJson(obj));

输出:

  {_ id:234242,name:carlos} 






编辑



如果你想合并3个JSON字符串,那么它将被存储为对象列表,如下所示:



示例代码:

 字符串json =[+ 
{\_id \:1,\name \:\A\},+
{\_id \:2,\ name \:\B\},+
{\_id \:3,\name \:\C \}+
];
Type type = new TypeToken< ArrayList< MyPOJO>>(){} .getType();
ArrayList< MyPOJO> obj = new Gson()。fromJson(json,type);
System.out.println(new GsonBuilder()。setPrettyPrinting()。create()。toJson(obj));

输出:

 < 

_id:1,
name:A
},
{
_id :2,
name:B
},
{
_id:3,
name:C
}
]


I have a different use case where fields in POJO itself stores JSON data, basically grouping of all data.

Now i want the complete JSON generated from above POJO.

At present i am using this method

private static Gson gson = new Gson();
public static String convertObjectToJSON(Object object) throws Exception {
    String jsonResponse = gson.toJson(object);
    return jsonResponse;
}

But getting output with escape characters around double quotes like below

 { \"_id\" : 234242, \"name\" : \"carlos\"}

I tried various options in GsonBuilder, but not working. Basically, i am just grouping multiple JSON data and sending it across.

Could you please do the needful help to get rid of the escape characters around double quotes.

UPDATE:

Question is : I have 3 JSONs and need to combine them into a single JSON and need to pass it to html5 client through Spring MVC. As of now i am added the 3 JSONs into a POJO and trying to convert the same to JSON. Same is explained above.

Thanks & Regards

Venkat

解决方案

I have tried below sample code and it doesn't have any escape characters around double quotes.

class MyPOJO{
    private int _id;
    private String name;
    // getter & setter
}

String json="{ \"_id\" : 234242, \"name\" : \"carlos\"}";
MyPOJOobj=new Gson().fromJson(json, MyPOJO.class);
System.out.println(new Gson().toJson(obj));

output:

{"_id":234242,"name":"carlos"}


EDIT

If you want to combine 3 JSON string then It will be stored as List of object as illustrated below:

sample code:

String json = "[" +
                    " { \"_id\" : 1, \"name\" : \"A\"}," +
                    " { \"_id\" : 2, \"name\" : \"B\"}," +
                    " { \"_id\" : 3, \"name\" : \"C\"}" +
                "]";
Type type = new TypeToken<ArrayList<MyPOJO>>() {}.getType();
ArrayList<MyPOJO> obj = new Gson().fromJson(json, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));

output:

[
  {
    "_id": 1,
    "name": "A"
  },
  {
    "_id": 2,
    "name": "B"
  },
  {
    "_id": 3,
    "name": "C"
  }
]

这篇关于GSON:如何将Object(其本身包含JSON)转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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