JSON GSON.fromJson Java对象 [英] JSON GSON.fromJson Java Objects

查看:836
本文介绍了JSON GSON.fromJson Java对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的Json加载到我的类中


$ b

  public User(){
this.fbId = 0;
this.email =;
this.name =;
this.thumb =;
this.gender =;
this.location =;
this.relationship = null;
this.friends = new ArrayList();



  {
用户:{
用户:{
名称:'姓名',
电子邮件:'omeome@email.com',
朋友:{
用户:{
名称:'别名',
电子邮件地址:'this@email.com',
朋友:{
用户:{
名称:'又一个名字',
email:'another@email.com'
}
}
}
}
}
}



$ b $ p
$ b我很努力让GSON将用户细节加载到上面的Java对象中以下代码

  User user = gson.fromJson(this.json,User.class); 


解决方案

JSON无效。集合不能由 {} 表示。它代表一个对象。集合/数组将由 [] 和逗号分隔的对象表示。



以下是JSON的外观:

  {
users:[{
name:name1,
email: email1,
朋友:[{
name:name2,
email:email2,
friends:[{
name:name3,
email:email3
},
{
name:name4,
email:email4
}]
} ]
}]
}

更深的嵌套好友的朋友,以便您了解如何在集合中指定多个对象)



鉴于此JSON,您的包装类应该看起来像这:

  public class Data {
private List< User>用户;
// + getters / setters
}

public class User {
private String name;
私人字符串电子邮件;
私人列表<用户>朋友;
// + getters / setters
}

然后转换它,使用

 数据数据= gson.fromJson(this.json,Data.class); 

并获取用户,请使用

 列表<使用者> users = data.getUsers(); 


I am trying to load my Json into my class

public User() {
    this.fbId = 0;
    this.email = "";
    this.name = "";
    this.thumb = "";
    this.gender = "";
    this.location = "";
    this.relationship = null;
    this.friends = new ArrayList();
}

{
    users:{
        user:{
            name:'the name',
            email:'some@email.com',
            friends:{
                user:{
                    name:'another name',
                    email:'this@email.com',
                    friends:{
                        user:{
                            name:'yet another name',
                            email:'another@email.com'
                        }
                    }
                }
            }
        }
    }
}

I am struggling to get GSON to load the user details into the above Java object with the following code

User user = gson.fromJson(this.json, User.class);

解决方案

The JSON is invalid. A collection is not to be represented by {}. It stands for an object. A collection/array is to be represented by [] with commaseparated objects.

Here's how the JSON should look like:

{
    users:[{
        name: "name1",
        email: "email1",
        friends:[{
            name: "name2",
            email: "email2",
            friends:[{
                name: "name3",
                email: "email3"
            },
            {
                name: "name4",
                email: "email4"
            }]
        }]
    }]
}

(note that I added one more friend to the deepest nested friend, so that you understand how to specify multiple objects in a collection)

Given this JSON, your wrapper class should look like this:

public class Data {
    private List<User> users;
    // +getters/setters
}

public class User {
    private String name;
    private String email;
    private List<User> friends;
    // +getters/setters
}

and then to convert it, use

Data data = gson.fromJson(this.json, Data.class);

and to get the users, use

List<User> users = data.getUsers();

这篇关于JSON GSON.fromJson Java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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