本文介绍了使用GSON解析JSON对象VS JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON要么是一个单独的对象或相同的对象的阵列。有没有一种方法来分析这些数据使用GSON它会在单个对象区分VS阵列?

唯一的解决办法我现在有这个是手动解析JSON和环绕的一个尝试捕捉。首先,我将尝试解析它作为一个单一的对象,如果失败的话,它会抛出一个异常,然后我会尝试解析它作为一个数组。

我不想分析它手动,但...这将需要我一辈子。这里是正在发生的事情的想法。

 公共类对象A实现Serializable {

    公共字符串变量;
    公共对象B []对象B; //或对象B对象B;

    公共对象A(){}
}
 

下面是这可以是一个数组或一个对象的对象。

 公共类对象B实现Serializable {

    公共字符串变量1;
    公共字符串变量2;

    公共对象B(){}
}
 

然后用JSON响应交互时。我这样做。

  GSON GSON =新GSON();
对象A []链表= gson.fromJson(响应,对象A []类。);
 

当对象A的数组被序列化,JSON的包含数组或单个对象的对象B。

  [
    {
        变量:等等等等,
        对象B:{
            变量1:1,
            变量2:2
        }
    },
    {
        变量:等等等等,
        对象B:{
            变量1:1,
            变量2:2
        }
    },
    {
        变量:等等等等,
        对象B:[
            {
                变量1:1,
                变量2:2
            },
            {
                变量1:1,
                变量2:2
            }
        ]
    }
]
 

解决方案

我只是改变了对象B [] 名单,其中,对象B> 对象A 声明。

 的ArrayList<对象A> LA =新的ArrayList<对象A>();
名单<对象A>名单=新GSON()fromJson(JSON,la.getClass())。

对于(对象一:名单)
{
   的System.out.println(一);
}
 

这是我的结果:

  {变量=等等等等,对象B = {变量1 = 1,变量2 = 2}}
{变量=等等等等,对象B = {变量1 = 1,变量2 = 2}}
{变量=等等等等,对象B = {[变量1 = 1,变量2 = 2},{变量1 = 1,变量2 = 2}]}
 

我认为,在全面仿制药的时代,如果没有特别的需求,可以从数组列表切换,你有很多好处,GSON也可以用做一个灵活的解析。

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?

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.

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.

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.

[
    {
        "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 just changed ObjectB[] to List<ObjectB> into ObjectA declaration.

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解析JSON对象VS JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 11:16