Parsing json with nested arrays with Gson

后端 未结 1 1699
旧巷少年郎
旧巷少年郎 2021-01-20 23:52

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\",
    \"         


        
1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 00:20

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

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

    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 entry;
          public List exit;
          public List> details;
          public List
    section; } public static class Section { public List> account; public String category; public String description; public String sectionname; public String id; } }

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

    EDIT

    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.

    0 讨论(0)
提交回复
热议问题