{
"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"
}
]
}
这个json怎么用对象解析
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
9条回答 默认 最新
- 晓呆同学 2015-12-17 04:16关注
给你写出了一种方式,也希望你能以后慢慢独立解决。
最外层定一个一个类,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解析的方法。
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 删除和修改功能无法调用
- ¥15 kafka topic 所有分副本数修改
- ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
- ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
- ¥40 串口调试助手打开串口后,keil5的代码就停止了
- ¥15 电脑最近经常蓝屏,求大家看看哪的问题
- ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
- ¥50 C++五子棋AI程序编写
- ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
- ¥15 SQL Server analysis services 服务安装失败