急急急急急急!!!!求大神帮我解析这个json字符串,小弟新人,求帮忙,什么方法都试了,还是出不来,,,有大神能帮我做出来吗??我搞不出来,要疯了 - - - -- -
{
"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"
}
]
}
var aUsage = { "int":"^([+-]?)//d+$", "int+":"^([+]?)//d+$", "int-":"^-//d+$", "num":"^([+-]?)//d*//.?//d+$", "num+":"^([+]?)//d*//.?//d+$", "num-":"^-//d*//.?//d+$", "float":"^([......
答案就在这里:求助,急急急!!!
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
建两个类:
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 java.util.List;
import net.sf.json.JSONArray;
public class Test {
public static void main(String[] args) {
String jsonStr = "[{\"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\"}]";
JSONArray jsonArray = JSONArray.fromObject(jsonStr);
List<Bill> billList = (List<Bill>) JSONArray.toCollection(jsonArray, Bill.class);
System.out.println(billList.size());
}
}
你复制过来的Json字符串格式有点问题,开始位置少了一个"[",结束位置多了一个"}",你检查一下。
使用的时候注意需要引入好几个Jar包。
已经测试OK,你可以直接使用。
另外这种解析Json格式的,大都不是很难,第一要保证Json格式正确,你可以在http://www.bejson.com/这里验证,然后你就观察Json格式的规则,一步一步来解析。
授之于鱼,不如授之以渔;在解析Json前,首先确保你的json串符合json规范,能够正确的转成Json对象。是否正确,可以在网上找一个校验工具,多得很,最好找一个可以格式化的,这里先提供一个简单可用的地址:http://tool.oschina.net/codeformat/json,校验成功之后,证明没有格式上没有问题,然后再去用相应的语言解析,这样就不会迷茫的不知所措了。