{
"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
package com.test;
import java.util.List;
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
package com.test;
import java.util.List;
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
package com.test;
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;
}
}
我們這樣來解析:
package com.test;
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");
}
}
在問答社區裡,回答過你之前的問題了,但是你的Json復制不全,給了你一個例子,今天又看到了你的問題,感覺應該是沒有靜心看下去,其實不難,也希望你能靜下心來好好學一學。
Json格式這麼多,希望你能通過幾個例子來掌握Json解析的方法。