GSON serialize sub Object

冷暖自知 提交于 2021-02-07 10:50:29

问题


I want to serialize this JSON structure :

{  
   "name":"name 1",
   "number":1,
   "standing":[  
      {  
         "subRank":1,
         "subname":"test"
      },
      {  
         "subRank":2,
         "subname":"Test2"
      }]
}

And I want to use an object association like that :

public class ParentClass{
    String name;
    String number;
    List<SubClass> SubClassList;
}

public class SubClass{
    String subRank;
    String subname;
}

I tried with this code :

Type type = new TypeToken<List<ParentClass>>() {}.getType();
ArrayList<ParentClass> parentClassList= new ArrayList<>();
parentClassList= gson.fromJson(jsonContent, type);

But i have this exception :

Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Thanks for your helps :)


回答1:


First of all, your json structure is not valid.

Remove last comma from below

     "subname":"Test2",

And your json structure is not a list, it is object structure and you try to parse it as list.

Change your code as:

ParentClass parentClassList= gson.fromJson(data.get(0), type);

Or change your json structure to array wrapping with "[]" symbols.

This works on me :

Gson gson = new Gson();

Type type = new TypeToken<ParentClass>() {}.getType();
ParentClass parentClassList= gson.fromJson(data, type);

System.out.println(parentClassList.name);

Json:

{"name":"name 1","number":1,"standing":[{"subRank":1,"subname":"test"},{"subRank":2,"subname":"Test2"}]}



回答2:


The approach that you are trying is for deserialize a collection (here you can read a very good example). But this is not your case.

If you want to serialize this json:

{  
   "name":"name 1",
   "number":1,
   "standing":[  
      {  
         "subRank":1,
         "subname":"test"
      },
      {  
         "subRank":2,
         "subname":"Test2"
      }]
}

The structure of your classes is not the correct one.

First of all, your number and subRank are not String.

Secondly, this one List<SubClass> SubClassList; have to be called standing.

So, the parent class will be:

public class ParentClass {
    String name;
    Number number;
    Collection<SubClass> standing;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Number getNumber() {
        return number;
    }

    public void setNumber(Number number) {
        this.number = number;
    }

    public Collection<SubClass> getStanding() {
        return standing;
    }

    public void setStanding(Collection<SubClass> standing) {
        this.standing = standing;
    }
}

And the sub class:

public class SubClass {
    Number subRank;
    String subname;

    public Number getSubRank() {
        return subRank;
    }

    public void setSubRank(Number subRank) {
        this.subRank = subRank;
    }

    public String getSubname() {
        return subname;
    }

    public void setSubname(String subname) {
        this.subname = subname;
    }
}

If you want to test:

public class Test {

    public static void main (String args[])
    {
        Gson gson = new Gson();

        SubClass sc = new SubClass();
        sc.setSubRank(1);
        sc.setSubname(("test"));

        SubClass sc2 = new SubClass();
        sc2.setSubRank(2);
        sc2.setSubname(("Test2"));
        LinkedList<SubClass> list= new LinkedList<>();

        list.add(sc);
        list.add(sc2);


        ParentClass pc = new ParentClass();
        pc.setName("name 1");
        pc.setNumber(1);
        pc.setStanding(list);

        System.out.println(gson.toJson(pc));
    }
}



回答3:


I did very similiar things with GSON.

  • Your standings are not a list. GSON parses them to an array.
  • If the member-names of your class differ to your json you need to annotate them.

Example below. Hope it points you in the right direction...


public class ParentClass{
    String name;
    String number;
    @SerializedName("standing")
    SubClass[] SubClassList;
}

public class SubClass{
    String subRank;
    String subname;
}



回答4:


I resolve my case like that :

JSON :

{  
   "name":"name 1",
   "number":1,
   "standing":[  
      {  
         "subRank":1,
         "subname":"test"
      },
      {  
         "subRank":2,
         "subname":"Test2"
      }]
}

POJO :

public class ParentClass{
    String name;
    int number;
    List<SubClass> standing;
}

public class SubClass{
    int subRank;
    String subname;
}

JAVA :

Gson gson = new Gson();
Type type = new TypeToken<ParentClass>() {}.getType();
ParentClass parentClassList= gson.fromJson(data, type);

Thanks very much @Emre, @Christoph-Tobias Schenke and @Davide Patti



来源:https://stackoverflow.com/questions/49964171/gson-serialize-sub-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!