开发者社区> 问答> 正文

这个json怎么用对象解析

{
"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"
}
]
}

展开
收起
蛮大人123 2016-03-24 17:20:48 2138 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    给你写出了一种方式,也希望你能以后慢慢独立解决。
    最外层定一个一个类,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");
            }
        }
    2019-07-17 19:13:09
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
神龙云服务器产品及技术深度解析 立即下载
弹性创造价值:基于ECS的最佳性价比实践解析 立即下载
又快又稳:阿里云下一代虚拟交换机解析 立即下载

相关镜像