Json字符串解析为具有多个对象的java对象
尝试使用 gson 将以下 json 字符串解析为 java 对象
{
"entry": "132456",
"product": {
"item": "123456",
"prompts": [
{
"promptId": "1",
"promptNumber": "109",
"promptType": 4,
"promptTypeDesc": "desc1",
"validations": [
{
"minLen": 10,
"maxLen": 10,
"required": true
}
]
},
{
"promptId": "2",
"promptNumber": "110",
"promptType": 4,
"promptTypeDesc": "desc2",
"validations": [
{
"minLen": 12,
"maxLen": 12,
"required": true
}
]
},
{
"promptId": "3",
"promptNumber": "72",
"promptType": 5,
"promptTypeDesc": "desc4",
"validations": [
{
"required": true
}
]
}
]
}
}
我有我的 java 类,因为
public class Info{
private String entry;
private Product product;
// added setters and getters
/* Product is inner class*/
public static Product {
private String item;
private List<Prompt> prompts;
// added getters and setters
/*Prompt inner class*/
public static Prompt{
private String promptId;
private String promptNumber;
private List<Validation> validations;
// getters and setters
/*Validation inner class*/
public static Validation{
private int maxLen;
private int minLen;
private boolean required;
// added getters and setters
} // end of Validation class
} // end of Prompt class
} // end of Product
} // End of Info
转换后我得到 Prompt 对象为 null。
item = gson.fromJson(response, Info.class);
有人可以纠正我吗
Trying to parse the following json string to java object using gson
{
"entry": "132456",
"product": {
"item": "123456",
"prompts": [
{
"promptId": "1",
"promptNumber": "109",
"promptType": 4,
"promptTypeDesc": "desc1",
"validations": [
{
"minLen": 10,
"maxLen": 10,
"required": true
}
]
},
{
"promptId": "2",
"promptNumber": "110",
"promptType": 4,
"promptTypeDesc": "desc2",
"validations": [
{
"minLen": 12,
"maxLen": 12,
"required": true
}
]
},
{
"promptId": "3",
"promptNumber": "72",
"promptType": 5,
"promptTypeDesc": "desc4",
"validations": [
{
"required": true
}
]
}
]
}
}
I have my java classes as
public class Info{
private String entry;
private Product product;
// added setters and getters
/* Product is inner class*/
public static Product {
private String item;
private List<Prompt> prompts;
// added getters and setters
/*Prompt inner class*/
public static Prompt{
private String promptId;
private String promptNumber;
private List<Validation> validations;
// getters and setters
/*Validation inner class*/
public static Validation{
private int maxLen;
private int minLen;
private boolean required;
// added getters and setters
} // end of Validation class
} // end of Prompt class
} // end of Product
} // End of Info
I am getting Prompt object as null after converting.
item = gson.fromJson(response, Info.class);
Can someone please correct me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试这个 JSON:
使用这个 Java 类:
像魅力一样工作
输出:
Try this JSON:
With this Java Class:
Works like a charm
Output: