Using Gson to parse Json array and object with no name

前端 未结 4 1111
执念已碎
执念已碎 2021-02-15 23:02

I know there are many JSON with GSON questions but none of them relate to me directly. My JSON is formatted differently.

I have a JSON data I want to parse using GSON wh

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-15 23:36

    According to your json, you should simply have :

    public class ItemArray extends List {
    }
    

    if you want to keep you java class and change your json it should be :

    {
     itemArray: [
         {
          "foo":"1",
          "bar":[ { "_id":"bar1"} ],
          "too":["mall", "park"]
         }
      ]
    }
    

    Oh, and there is a mismatch with the id and _id for Bar :

    public class Bar
    {
       String _id;
    
       //Get set here
    }
    

    You could also use an annotation to change the field's name during Json de/serialization.

    And last but not least, consider typing your values better. Don't see any data as strings if they are not, you will not a lot of processing in java code to convert things. For instance :

    "foo" : 1,
    

    and see foo as an int data member, not a String.

提交回复
热议问题