java - 使用 Gson 解析带有嵌套数组的 json

标签 java android json gson

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

{"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"
}

我试着用下面的方式解析它

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);

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

最佳答案

首先,您的 JSON 是错误的,这里是更正后的版本(例如,请注意代码第 9 行中的额外逗号)。

{
  "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/ .

其次,Gson不太喜欢内部类,除非声明为static。

第三:避免在你的类中混用数组和泛型,泛型使用起来更安全,所以我重新定义了你的类如下:

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 关键字)。

编辑

正如 Brian Roach 在评论中指出的那样,内部类不再需要是静态的就可以与 Gson 很好地协同工作。所以第 2 点不再成立,您可以从类声明中删除静态。

关于java - 使用 Gson 解析带有嵌套数组的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21050346/

相关文章:

android - 带 opengles 的经纱纹理

Python Pyramid 解析 json

ios - Swift - NSJsonSerialization - "Unable to convert data to string around character"错误

apache - 批量导入 json 文档到 Apache CouchDb

java - Intellij Idea - 在网上查找 jar - 下载失败

java - 为什么这个MySQL不能正确执行?

java - 为什么逆整数(Leet Code)的解法是 O((log10(n))?

android - 葫芦 android.util.AndroidException : INSTRUMENTATION_FAILED

android - 将Android Studio 3.5升级到3.6.1后编译应用时出错

Java - Runtime.exec() 无法运行