简体   繁体   English

使用 gson 反序列化内部类返回 null

[英]Deserializing inner class with gson returns null

I want to use Gson to Deserialize my JSON into objects.我想使用 Gson 将我的 JSON 反序列化为对象。 I've defined the appropriate classes, and some of those class' objects are included in other objects.我已经定义了适当的类,其中一些类的对象包含在其他对象中。 When trying to deserialize the whole JSON, I got null values, so I started breaking it apart.在尝试反序列化整个 JSON 时,我得到了空值,因此我开始将其分解。

I reached the point where all lower classes stand by them selves, but when trying to deserialize into an object that holds an instance of that smaller object - every thing returns as null.我达到了所有较低类都支持他们自己的地步,但是当尝试反序列化为一个包含该较小对象实例的对象时 - 每件事都返回为 null。

My partial JSON:我的部分 JSON:

{
  "user_profile": {
    "pk": 1,
    "model": "vcb.userprofile",
    "fields": {
      "photo": "images/users/Screen_Shot_2013-03-18_at_5.24.13_PM.png",
      "facebook_url": "https://google.com/facebook",
      "site_name": "simple food",
      "user": {
        "pk": 1,
        "model": "auth.user",
        "fields": {
          "first_name": "blue",
          "last_name": "bla"
        }
      },
      "site_url": "https://google.com/"
    }
  }
}

UserProfile Class:用户配置文件类:

public class UserProfile {
    private int pk;
    private String model;
    private UPfields fields = new UPfields();//i tried with and without the "new"
}

UPfields Class: UPfields 类:

public class UPfields {
    private String photo;
    private String facebook_url;
    private String site_name;
    private User user;
    private String site_url;
}

User Class:用户类别:

public class User {
    private int pk;
    private String model;
    private Ufields fields;
}

Ufields Class: Ufields 类:

public class Ufields {
    private String first_name;
    private String last_name;
}

In my main I call:在我的主要我打电话:

Gson gson = new Gson();
UserProfile temp = gson.fromJson(json, UserProfile.class);

So my temp object contain only null values.所以我的临时对象只包含空值。 I've tried changing the classes to static, and it doesn't work.我试过将类更改为静态,但它不起作用。 The UPfields object and all lower one work fine. UPfields 对象和所有较低的对象都可以正常工作。

Any suggestions?有什么建议?

when I remove the当我删除

"{
  "user_profile":"

and it's closing bracket, the deserialize to a user_profile object works.它是右括号,反序列化为 user_profile 对象有效。

In order to parse this json example you have to create auxiliary class, which will contain field named user_profile of type UserProfile :为了解析这个json示例,您必须创建辅助类,该类将包含名为UserProfile类型的user_profile字段:

public class UserProfileWrapper {
    private UserProfile user_profile;
}

and parse this json string with this class:并使用此类解析此json字符串:

UserProfileWrapper temp = gson.fromJson(json, UserProfileWrapper.class);

Gson starts by parsing the outermost object, which in your case has a single field, user_profile . Gson 首先解析最外面的对象,在您的情况下,它只有一个字段user_profile Your UserProfile class doesn't have a user_profile field, so it can't deserialize it as an instance of that class.您的UserProfile类没有user_profile字段,因此无法将其反序列化为该类的实例。 You should try to deserialize the value of the user_profile field instead.您应该尝试反序列化user_profile字段的

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

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