{
"code": " D1_3300_0000",
"action": "prepay",
"title": "缴费 - 浙江电力",
"form": [
{
"type": "bill",
"name": "bill_id",
"label": "您查询账单如下",
"options": [
{
"label": "2013年5月",
"value": "201301011530008001140",
"amount": "13.58"
},
{
"label": "2013年6月",
"value": "201301011530008001141",
"amount": "23.47"
}
]
},
{
"type": "string",
"label": "户名",
"value": "张三"
},
{
"type": "string",
"label": "地址",
"value": "杭州市西湖区玉泉路201号"
},
{
"type": "string",
"label": "违约金(元)",
"value": "0"
},
{
"type": "string",
"label": "总应缴金额(元)",
"value": "30.23"
}
]
}
给你写出了一种方式,也希望你能以后慢慢独立解决。
最外层定一个一个类,Fees.java
public class Fees {
private String code;
private String action;
private String title;
private List<Bill> form;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Bill> getForm() {
return form;
}
public void setForm(List<Bill> form) {
this.form = form;
}
}
第二层的类,Bill.java
public class Bill {
private String type;
private String name;
private String label;
private List<Option> options;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public List<Option> getOptions() {
return options;
}
public void setOptions(List<Option> options) {
this.options = options;
}
}
最内层类,Option.java
public class Option {
private String label;
private String value;
private String amount;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
我们这样来解析:
import net.sf.json.JSONObject;
public class Test {
public static void main(String[] args) {
String jsonStr = "{\"code\": \" D1_3300_0000\",\"action\": \"prepay\",\"title\": \"缴费 - 浙江电力\",\"form\":[{\"type\": \"bill\",\"name\": \"bill_id\",\"label\": \"您查询账单如下\",\"options\": [{\"label\": \"2013年5月\",\"value\": \"201301011530008001140\",\"amount\": \"13.58\"},{\"label\": \"2013年6月\",\"value\": \"201301011530008001141\",\"amount\": \"23.47\"}]},{\"type\": \"string\",\"label\": \"户名\",\"value\": \"张三\"},{\"type\": \"string\",\"label\": \"地址\",\"value\": \"杭州市西湖区玉泉路201号\"},{\"type\": \"string\",\"label\": \"违约金(元)\",\"value\": \"0\"},{\"type\": \"string\",\"label\": \"总应缴金额(元)\",\"value\": \"30.23\"}]}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
Fees fee = (Fees) JSONObject.toBean(jsonObject, Fees.class);
System.out.println("asdf");
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。