简体   繁体   English

用Gson反序列化对象Json的列表

[英]Deserialize List of object Json with Gson

I have the next Json : 我有下一个杰森:

{"total":1,"min":20,"sort":"","count":20,"houses":[{"logo":"house1.jpg","name":"Springfield","age":100}]

The class House is the next : 下一个类是House:

public class House{
    private String logo;
    private String name;
    private int age;
}

And I want to obtain the list of House. 我想获得众议院名单。 I tried with the next code : 我尝试了下一个代码:

Gson gson = new Gson();
List<House> rest = gson.fromJson(JsonString, new TypeToken<List<House>>(){}.getType());

But it doesnt work, the progam is in stuck. 但是它不起作用,程序卡住了。

Thanks in advance 提前致谢

If i were you i create a new class such as: 如果我是你,我会创建一个新类,例如:

public class YourNewObject
{
    private int total;
    private int min;
    private int count
    private List<House> houses;
}

And then use this: 然后使用:

Gson gson = new Gson();
YourNewObject houses =  gson.fromJson(JsonString, YourNewObject.class);

That should work. 那应该工作。

Regards 问候

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

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