使用Gson将JsonArray序列化为数组而不是对象 [英] Serializing JsonArray as array and not object using Gson

查看:75
本文介绍了使用Gson将JsonArray序列化为数组而不是对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Gson库,但是当它序列化JsonArray对象时,似乎将其序列化为对象而不是JSON数组.即

I'm using Gson library but when it serializes the JsonArray object, it seems to serialize this as an object rather than a JSON array. i.e.

{元素:[{{name:"value1},{name:" value2}]}

{ elements: [ {name:"value1}, {name:"value2"}]}

如何从序列化中删除元素?

How do I remove the elements from being serialized?

推荐答案

我去看医生,因为走路时脚受伤了.医生说:不要踩它."

I went to see the doctor, because my foot hurt when I walked on it. The doctor said, "Don't walk on it."

通常,当使用像Gson这样的API时,甚至不愿知道 JsonArray 的存在,而他们只是使用API​​的数据绑定部分.因此,与其手动构建 JsonArray 然后进行序列化,不如将Java List 或数组提供给 Gson.toJson().例如:

Generally, when working with an API like Gson, one would rather not even know that JsonArray exists, and they'd instead just use the data binding part of the API. So, instead of manually building a JsonArray, and then deal with serializing it, just feed a Java List or array to Gson.toJson(). For example:

List list = new ArrayList();
list.add("one");
list.add(2);
list.add(new Foo());

Gson gson = new Gson();
String json = gson.toJson(list);
System.out.println(json);

如果这种方法不能满足您的需求,并且由于某种原因您被困在使用 JsonArray 的话,那么您可能会想调用它的 toString()方法,因为当前确实可以创建最终想要的方法,所以我不会使用它,因为文档中没有任何内容可以保证 toString()可以创建封闭数组的有效JSON表示形式内容.因此,在将来的Gson版本中,它可能不会返回相同格式的 String .

If that approach doesn't fit your needs and you're stuck using a JsonArray for some reason, then you might be tempted to just call its toString() method, since that does currently create what's ultimately desired, I wouldn't use it, because there is nothing in the documentation that says the toString() is guaranteed to create a valid JSON representation of the enclosed array contents. So, it might not return a String of the same format in future releases of Gson.

无论如何,如果您真的想使用JsonArray,它应该如下进行足够的序列化.

At any rate, if you really want to use a JsonArray, it should serialize well enough as follows.

JsonElement one = new JsonPrimitive("one");
JsonElement two = new JsonPrimitive(2);
JsonObject foo = new JsonObject();
foo.addProperty("foo", new Foo().foo);

JsonArray jsonArray = new JsonArray();
jsonArray.add(one);
jsonArray.add(two);
jsonArray.add(foo);

System.out.println(new Gson().toJson(jsonArray));
// ["one",2,{"foo":"hi"}]

注意:此答案基于Gson 2.2 API.我不记得早期版本的Gson是否包含重载的 toJson(JsonElement)方法.

Note: This answer is based on the Gson 2.2 API. I don't recall whether earlier versions of Gson included the overloaded toJson(JsonElement) methods.

如果已经以这种方式使用了 toJson 方法(序列化 JsonArray ),但是输出如原始问题所示,请回想一下Java没有在重载方法中进行选择时,请不要考虑运行时类型.它绑定到编译时间类型.(我知道.)因此,您可能需要将参数类型强制转换为 JsonElement ,以使编译器知道绑定到哪种方法.以下内容说明了原始问题中可能发生的情况.

If the toJson method is already being used in this fashion (to serialize a JsonArray), but the output is as demonstrated in the original question, recall that Java doesn't consider the runtime type when selecting amongst overloaded methods. It binds to the compile time type. (Lame -- I know.) So, you may need to cast the argument type to JsonElement, to let the compiler know which method to bind to. The following demonstrates what might be effectively happening in the original question.

System.out.println(new Gson().toJson((Object)jsonArray));
// {"elements":["one",2,{"foo":"hi"}]}

这篇关于使用Gson将JsonArray序列化为数组而不是对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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