Java - Gson解析嵌套在嵌套 [英] Java - Gson parsing nested within nested
本文介绍了Java - Gson解析嵌套在嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须与一个API进行交互,并且响应格式(从我读过的)看起来结构很差。我发现一个Google群组回复了一个有点类似的问题:这里,但我无法实现一个Response类来处理Gson.fromJson。
I have to interact with an API, and the response format (from what I've read) seems to be poorly structured. I've found a google groups reply to a somewhat similiar problem here, but I'm having trouble implementing a Response class to handle the Gson.fromJson. Is there an example I'm missing that's out there?
{
"response":{
"reference": 1023,
"data":{
"user":{
"id":"210",
"firstName":"john",
"lastName":"smith",
"email":"pocahontas@gmail.com",
"phone":"",
"linkedid":{
"id":"238"
}
}
}
}
}
推荐答案
可以表示JSON对象 {}
通过 Map< String,Object>
或一个Javabean类。这是一个使用Javabean的例子。
The JSON objects {}
can be represented by a Map<String, Object>
or a Javabean class. Here's an example which uses a Javabean.
public class ResponseData {
private Response response;
// +getter+setter
public static class Response {
private int reference;
private Data data;
// +getters+setters
}
public static class Data {
private User user;
// +getter+setter
}
public static class User {
private String id;
private String firstName;
private String lastName;
private String email;
private String phone;
private Linkedid linkedid;
// +getters+setters
}
public static class Linkedid {
private String id;
// +getter+setter
}
}
使用它如下:
Use it as follows:
ResponseData responseData = new Gson().fromJson(json, ResponseData.class);
这篇关于Java - Gson解析嵌套在嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文