{
"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解析的方法。
怎么用对象来接受,求代码,搞了一天多,还是不行,
三级层次关联一对多的mapping,建三个对象,第一个对象form包含第二个对象的list,第二个对象options包含第二个对象的list
(一)在网络数据传输中,经常用到Json或者xml,两者在可读性、解析手段上均比较好,但是Json优势是体积小,与JavaScript交互方便,缺点是对数据的可描述性比较差。
(二)Json由一系列的键值对的集合或 数组对象。
(三)例子:{"name”:"zhangsan","age":10}或者[{"name”:......
答案就在这里:Json对象解析
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
随便下个json的jar包,然后就能解析了
是解析成Java对象还是解析成javascript对象,还是其它语言对象?
java用Jackson
javascript 用JSON.stringfy();
用递归解析,就是一个对象一直递归下去
建议复杂结构json,,,可以使用JsonFormat工具解析,,,1s.
谢谢你了.我会好好努力的,新手