简体   繁体   English

GSON序列化子对象

[英]GSON serialize sub Object

I want to serialize this JSON structure : 我想序列化此JSON结构:

{  
   "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 $ 线程“主”中的异常com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2列路径$ BEGIN_OBJECT

Thanks for your helps :) 感谢您的帮助:)

First of all, your json structure is not valid. 首先,您的json结构无效。

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. 而且您的json结构不是列表,而是对象结构,您尝试将其解析为列表。

Change your code as: 将代码更改为:

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

Or change your json structure to array wrapping with "[]" symbols. 或将您的json结构更改为使用“ []”符号包装的数组。

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

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: 如果要序列化此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 . 首先,您的numbersubRank不是String

Secondly, this one List<SubClass> SubClassList; 其次,这个List<SubClass> SubClassList; have to be called standing . 必须被称为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));
    }
}

I did very similiar things with GSON. 我和GSON做过非常相似的事情。

  • Your standings are not a list. 您的排名不是清单。 GSON parses them to an array. GSON将它们解析为一个数组。
  • If the member-names of your class differ to your json you need to annotate them. 如果类的成员名称与json不同,则需要对其进行注释。

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

I resolve my case like that : 我这样解决我的案件:

JSON : JSON

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

POJO : POJO

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

public class SubClass{
    int subRank;
    String subname;
}

JAVA : 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 非常感谢@ Emre,@ Christoph-Tobias Schenke和@Davide Patti

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM