将JsonArray添加到JsonObject生成的转义字符(gson) [英] Adding JsonArray to JsonObject generated escape characters (gson)

查看:1268
本文介绍了将JsonArray添加到JsonObject生成的转义字符(gson)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 GSON 库创建json对象并为其添加json数组。我的代码看起来像这样:

  JsonObject main = new JsonObject(); 
main.addProperty(KEY_A,a);
main.addProperty(KEY_B,b);

Gson gson = new Gson();
ArrayList< JsonObject> list = new ArrayList<>();
JsonObject objectInList = new JsonObject();
objectInList.addProperty(KEY_C,c);
objectInList.addProperty(KEY_D,d);
objectInList.addProperty(KEY_E,e);
list.add(objectInList);
main.addProperty(KEY_ARRAY,gson.toJson(list));

输出似乎包含一些意想不到的斜杠:

  { A: 一个, b: b, 阵列:[ {\ c\ :\ c\ ,\D \:\d \,\E \:\e \}]} 


解决方案

当您执行以下操作时:

  main.addProperty(KEY_ARRAY,gson.toJson(list)); 

您添加一个键值对字符串 - > JsonObject 中的字符串,而不是字符串 - > JsonArray [的JSONObject]

现在你得到了这个斜杠,因为当Gson将这个List序列化为一个String时,它保存了关于数组中json对象值的信息(即字符串,引号需要通过反斜杠进行转义)。



您可以通过设置

  Gson gson =新的GsonBuilder()。setPrettyPrinting()。create(); 

然后数组的输出是:

 array:[\ n {\ n \C \:\c \,\\\
\D\ :\d \,\\\
\E \:\e \\\\
} \\\
]

但是你要找的是正确的映射,你需要使用 add 方法并给一个 JsonArray 作为参数。因此,将 list 更改为 JsonArray

  JsonArray list = new JsonArray(); 

并使用添加而不是 addProperty



main.add(array,list);



,您将获得输出:

  { A:a,B:b,array:[{C:c,D:d,E:e}]} 


I'm using GSON library to create a json object and add a json array to it. My code looks something like this:

JsonObject main = new JsonObject();
main.addProperty(KEY_A, a);
main.addProperty(KEY_B, b);

Gson gson = new Gson();
ArrayList<JsonObject> list = new ArrayList<>();
JsonObject objectInList = new JsonObject();
objectInList.addProperty(KEY_C, c);
objectInList.addProperty(KEY_D, d);
objectInList.addProperty(KEY_E, e);
list.add(objectInList);
main.addProperty(KEY_ARRAY,gson.toJson(list));

The output seems to contain some unexpected slashes:

{"A":"a","B":"b","array":["{\"C\":\"c\",\"D\":\"d\",\"E\":\"e\"}"]}

解决方案

When you do:

main.addProperty(KEY_ARRAY, gson.toJson(list));

you add a key-value pair String -> String in your JsonObject, not a String -> JsonArray[JsonObject].

Now you get this slashes because when Gson serialize this List into a String, it keeps the informations about the values in the json object in the array (that are Strings so the quotes need to be escaped via backslashes).

You could observe the same behavior by setting

Gson gson = new GsonBuilder().setPrettyPrinting().create();

then the output of the array is:

"array": "[\n  {\n    \"C\": \"c\",\n    \"D\": \"d\",\n    \"E\": \"e\"\n  }\n]"

But what you are looking for is to have the correct mapping, you need to use the add method and give a JsonArray as parameter. So change your list to be a JsonArray:

JsonArray list = new JsonArray();

and use add instead of addProperty:

main.add("array", list);

and you'll get as output:

{"A":"a","B":"b","array":[{"C":"c","D":"d","E":"e"}]}

这篇关于将JsonArray添加到JsonObject生成的转义字符(gson)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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