Gson调用会在多个类上返回空对象 [英] Gson calls returns empty objects over multiple classes

查看:70
本文介绍了Gson调用会在多个类上返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化以序列化下面包含的json字符串.

I'm trying to serialize to serialize the json string I have included below.

 {
        "mood": {
            "is_featured": true,
            "description": null,
            "title": "2014 ",
            "ordering": null,
            "is_recently_modified": true,
            "is_test": false,
            "tracks": [

            {
                "album": {
                    "release_date": "2014-11-06",
                    "id": 359778,
                    "name": "Amansız Gücenik"
                },
                "name": "Hırpalandı Mayıs",
                "artist": {
                    "id": 491169,
                    "name": "Ceylan Ertem"
                },
                "duration": 227,
                "isrc": "TRA161400207",
                "id": 3903997
            },
            {
                "album": {
                    "release_date": "2013-08-05",
                    "id": 329129,
                    "name": "For Fuld Musik - 25 Danske Sommer Pop & Rock Hits Vol. 2"
                },
                "name": "Am I Wrong",
                "artist": {
                    "id": 755957,
                    "name": "Nico & Vinz"
                },
                "duration": 387,
                "isrc": "NO2G31301011",
                "id": 3655085
            }
        ],
        "image_url": "some_url",
        "is_recently_created": true,
        "id": 128
    }
}

我正在使用此gson调用对其进行序列化

I'm using this gson call to serialize it

Mood mood = new Gson().fromJson(result, Mood.class);

我的班级讲师是这样的.

My class structers are like this.

public class Mood {

    private boolean is_featured;
    private boolean is_recently_modified;
    private boolean is_recently_created;
    private boolean is_test;


    private String  description;
    private String  title;
    private String  image_url;

    private int     id;
    private int     ordering;

    private Track[] tracks;



    public static class MoodContainer {
        public Mood[] moods;
    }


}


public class Track {

    //variables
    private Album       album;
    private Artist      artist;
    private Provider    provider;


    private String      secure_url;
    private String      name;
    private String      region;
    private String      isrc;

    private int         duration;
    private int         track_order;
    private int         id;

}

对于其他任何类变量,它都是这样.当我尝试使用上述调用时,最终会得到具有所有空值的对象.要注意的一件事是json字符串中未提供某些字段,因为不同的api调用提供了这些json字符串的不同部分.我做错了什么?

And it goes on like this for any additional class variable. When I try to use the above call I end up with objects that have all null values. One thing to notice is some fields are not supplied in json string because different api calls supply different parts of these json strings. What I am doing wrong?

推荐答案

您提供的Root JSON对象具有属性mood-因此,您有两个选择可以使反序列化正常工作:

Root JSON object you provided has property mood - so you either have two options for deserialization to work properly:

  1. Mood类包装在另一个对象中,如下所示:

  1. Wrap your Mood class inside another object like this:

public class MoodWrapper { private Mood mood; }

并将反序列化代码更改为

and change de-serialization code to

MoodWrapper moodWrapper = new Gson().fromJson(result, MoodWrapper.class);

反序列化时跳过根对象:

Skip a root object when deserializing:

final Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject rootObj = parser.parse(json).getAsJsonObject();
Mood mood = gson.fromJson(rootObj.getAsJsonObject("mood"), Mood.class);

这篇关于Gson调用会在多个类上返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆