用Gson解析带有嵌套数组的json [英] Parsing json with nested arrays with Gson

查看:171
本文介绍了用Gson解析带有嵌套数组的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Gson解析的新手,几乎没有进行基本的Gson解析.但是这次我的JSON非常复杂.我的JSON看起来像:

I am new to Gson parsing and did few basic Gson parsing. But this time my JSON is much complex. My JSON looks like :

{"uname":"man101",
"uid":"2",
    "account":{
            "entry":[8,15.48],
            "exit":[8,15.48],
            "details":
                [[0,0],[0,8.2],[1.15,8.2],[1.15,18.23],[7.33,18.23],[7.33,15.48],[12.15,2.28],
                [12.35,2.28],[12.35,0],[10.65,0],[10.65,1.42],[8.1,1.42],[8.1,3.95],
                [4.25,3.95],[4.25,0]],

            "section":
                [
                   {
                        "account":[[0,0],[0,3.35],
                            [4.25,3.35],[4.25,0]],
                            "category":"office",
                           "description":"Mobile based company",
                           "sectionname":"xyz",
                           "id":1
                  },

                  {
                        "account":[[0,3.95],[0,7.8],
                              [4.25,7.8],4.25,3.95]],
                        "category":"office",
                        "description":"Network based company",
                        "sectionname":"ABC",
                        "id":2
                  },
                ]
            },
    "category":"Cowork",
    "description":"Combined office space"
}

我试图通过以下方式解析

And I tried to parse this in following way

public class AccountData
{
    public String uname;
    public String uid;
    public String category;
    public String description;
    public Account account;

    public class Account
    {
        public float[] entry;
        public float[] exit;
        public List<float[]> details;
        public List<Section> section;
    }

    public class Section
    {
        public List<float[]> account;
        public String category;
        public String description;
        public String sectionname;
        public String id;
    }

}

并尝试通过这样的结果

 Gson gson = new Gson();
 beaconList = gson.fromJson(result, AccountData.class);

它运行时没有任何错误,但是当我尝试访问某些数据时,它给出了空值.

It's run without any error but when I tried to access some data it gives null values.

推荐答案

首先,首先,您的JSON错误,这是已更正的版本(请注意,例如,您的第9行中的多余逗号代码).

First of all, your JSON is wrong, here is the corrected version (note for example the extra comma in line 9 of your code).

{
  "uname": "man101", 
  "uid": "2", 
  "account": {
    "entry": [
      8, 
      15.48
    ], 
    "exit": [
      8, 
      15.48
    ], 
    "details": [
      [
        0, 
        0
      ], 
      [
        0, 
        8.2
      ], 
      [
        1.15, 
        8.2
      ], 
      [
        1.15, 
        18.23
      ], 
      [
        7.33, 
        18.23
      ], 
      [
        7.33, 
        15.48
      ], 
      [
        12.15, 
        2.28
      ], 
      [
        12.35, 
        2.28
      ], 
      [
        12.35, 
        0
      ], 
      [
        10.65, 
        0
      ], 
      [
        10.65, 
        1.42
      ], 
      [
        8.1, 
        1.42
      ], 
      [
        8.1, 
        3.95
      ], 
      [
        4.25, 
        3.95
      ], 
      [
        4.25, 
        0
      ]
    ], 
    "section": [
      {
        "account": [
          [
            0, 
            0
          ], 
          [
            0, 
            3.35
          ], 
          [
            4.25, 
            3.35
          ], 
          [
            4.25, 
            0
          ]
        ], 
        "category": "office", 
        "description": "Mobile based company", 
        "sectionname": "xyz", 
        "id": 1
      }, 
      {
        "account": [
          [
            0, 
            3.95
          ], 
          [
            0, 
            7.8
          ], 
          [
            4.25, 
            7.8
          ], 
          [
            4.25, 
            3.95
          ]
        ], 
        "category": "office", 
        "description": "Network based company", 
        "sectionname": "ABC", 
        "id": 2
      }
    ]
  }, 
  "category": "Cowork", 
  "description": "Combined office space"
}

您可以使用 http://json.parser.online.fr/来检查json,或者 http://www.bodurov.com/JsonFormatter/.

You can check your json with http://json.parser.online.fr/ or http://www.bodurov.com/JsonFormatter/.

第二,Gson不太喜欢内部类,除非内部类被声明为静态.

Second, Gson does not like inner classes so much, unless they are declared static.

第三:避免在您的类中混合使用数组和泛型,泛型更安全,因此我将您的类重新定义如下:

and third: avoid mixing arrays and generics in your classes, generics are safer to use, so I redefined your class as follows:

public class AccountData {
   public String uname;
   public String uid;
   public String category;
   public String description;
   public Account account;



   public static class Account {
      public List<Double> entry;
      public List<Double> exit;
      public List<List<Double>> details;
      public List<Section> section;
   }

   public static class Section {
      public List<List<Double>> account;
      public String category;
      public String description;
      public String sectionname;
      public String id;
   }

}

如果您不喜欢内部静态类,则可以始终将SectionAccount放入单独的文件中(当然,没有static关键字).

If you don't like inner static classes you can always put Section and Account into separate files (without static keyword, of course).

编辑

正如布赖恩·罗奇(Brian Roach)在评论中指出的那样,不再需要内部类是静态的就可以与Gson一起良好地工作.因此,第2点不再成立,您可以从类声明中删除static.

As Brian Roach pointed out in comments, it's not needed anymore for inner classes to be static to work well with Gson. So point 2 is no true anymore, you can remove static from your classes declaration.

这篇关于用Gson解析带有嵌套数组的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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