Java – Use Gson to parse JSON with nested arrays

Use Gson to parse JSON with nested arrays… here is a solution to the problem.

Use Gson to parse JSON with nested arrays

I’m new to gson parsing and have barely done basic gson parsing. But this time my JSON is very complicated. My JSON looks like this:

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

I tried to parse it in the 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;
    }

}

And try to pass the result like this

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

It runs without any errors, but it gives null when I try to access some data.

Solution

First, your JSON is wrong, here is the corrected version (for example, note the extra comma in line 9 of the 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 using http://json.parser.online.fr/ http://www.bodurov.com/JsonFormatter/ .

Second, Gson doesn’t like inner classes very much, unless declared static.

And third: avoid mixing arrays and generics in your class, 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;
   }

}

If you don’t like internal static classes, you can always put Section and Account in separate files (without the static keyword, of course).

Edit

As Brian Roach points out in his review, internal classes no longer need to be static to work well with Gson. So point 2 no longer holds, you can remove static from the class declaration.

Related Problems and Solutions