Java 使用 GSON 解析 json 对象 vs json 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18993414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using GSON to parse json object vs json array
提问by abounket
I have a JSON that is either a single object or an array of the same object. Is there a way to parse this data using Gson where it'll distinguish between the single object vs the array?
我有一个 JSON,它要么是单个对象,要么是同一对象的数组。有没有办法使用 Gson 解析这些数据,它可以区分单个对象与数组?
The only solution I currently have for this is to manually parse the json and surround that with a try catch. First I'll try parsing it as a single object, if it fails, it'll throw an exception and then I'll try to parse it as an array.
我目前唯一的解决方案是手动解析 json 并用 try catch 包围它。首先,我将尝试将其解析为单个对象,如果失败,则会抛出异常,然后我将尝试将其解析为数组。
I don't want to parse it manually though...that would take me forever. Here's an idea of what's happening.
不过,我不想手动解析它……那将永远花费我。这是正在发生的事情的想法。
public class ObjectA implements Serializable{
public String variable;
public ObjectB[] objectb; //or ObjectB objectb;
public ObjectA (){}
}
Here's the object that can either be an array or a single object.
这是可以是数组或单个对象的对象。
public class ObjectB implements Serializable{
public String variable1;
public String variable2;
public ObjectB (){}
}
And then when interacting with the json response. I'm doing this.
然后在与 json 响应交互时。我正在做这个。
Gson gson = new Gson();
ObjectA[] objectList = gson.fromJson(response, ObjectA[].class);
When the array of ObjectA's are being serialized, the json contains either an array or single object for ObjectB.
当 ObjectA 的数组被序列化时,json 包含 ObjectB 的数组或单个对象。
[
{
"variable": "blah blah",
"objectb": {
"variable1": "1",
"variable2": "2"
}
},
{
"variable": "blah blah",
"objectb": {
"variable1": "1",
"variable2": "2"
}
},
{
"variable": "blah blah",
"objectb": [
{
"variable1": "1",
"variable2": "2"
},
{
"variable1": "1",
"variable2": "2"
}
]
}
]
采纳答案by giampaolo
I just changed ObjectB[]
to List<ObjectB>
into ObjectA
declaration.
我刚换ObjectB[]
到List<ObjectB>
成ObjectA
声明。
ArrayList<ObjectA> la = new ArrayList<ObjectA>();
List<ObjectA> list = new Gson().fromJson(json, la.getClass());
for (Object a : list)
{
System.out.println(a);
}
and this is my result:
这是我的结果:
{variable=blah blah, objectb={variable1=1, variable2=2}}
{variable=blah blah, objectb={variable1=1, variable2=2}}
{variable=blah blah, objectb=[{variable1=1, variable2=2}, {variable1=1, variable2=2}]}
I think that in full generics era, if you do not have particular needs, you can switch from arrays to lists, you have many benefits that Gson also can use to do a flexible parsing.
我认为在全泛型时代,如果你没有特别的需求,你可以从数组切换到列表,你有很多好处 Gson 也可以用来做灵活的解析。
回答by Selecsosi
Can you try/catch it by first trying to parse the array, then falling back to parsing the single object class?
您可以通过首先尝试解析数组,然后回退到解析单个对象类来尝试/捕获它吗?
You could also do a real simple test and look to the first non whitespace character in the string you are deseralizing, if it is a "{" it is a single object, if it is a "[" it is an array
您还可以做一个真正简单的测试并查看您要反序列化的字符串中的第一个非空白字符,如果它是“{”,则它是单个对象,如果它是“[”,则它是一个数组
回答by Selecsosi
Try to use com.google.gson.JsonParser.
尝试使用 com.google.gson.JsonParser。
String jsonString = "object json representation";
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(jsonString);
if (jsonElement.isJsonArray()) {
// some logic
}
There are different ways to get your object using the JsonElement instance, for example - simply using the com.google.gson.Gson methods :
例如,有多种方法可以使用 JsonElement 实例获取对象 - 只需使用 com.google.gson.Gson 方法:
public <T> T fromJson(com.google.gson.JsonElement json, java.lang.Class<T> classOfT) throws com.google.gson.JsonSyntaxException
public <T> T fromJson(com.google.gson.JsonElement json, java.lang.reflect.Type typeOfT) throws com.google.gson.JsonSyntaxException
So, study the JsonElement, JsonArray, JsonPrimitive, JsonNull and JsonObject classes. Believe, they have an adequate interface to recover your object.
因此,研究 JsonElement、JsonArray、JsonPrimitive、JsonNull 和 JsonObject 类。相信,他们有足够的接口来恢复您的对象。