zoukankan      html  css  js  c++  java
  • json 解析

    package com.jsontest.demotest;
    
    
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    
    /**
     * 使用json-lib构造和解析Json数据
     * 
     * @author Alexia
     * @date 2013/5/23
     *
     */
    public class JsonTest {
    
        /**
         * 构造Json数据
         * 
         * @return
         */
        public static String BuildJson() {
    
            // JSON格式数据解析对象
            JSONObject jo = new JSONObject();
    
            // 下面构造两个map、一个list和一个Employee对象
            Map<String, String> map1 = new HashMap<String, String>();
            map1.put("name", "aaa");
            map1.put("sex", "female");
            map1.put("age", "23");
    
            Map<String, String> map2 = new HashMap<String, String>();
            map2.put("name", "bbb");
            map2.put("sex", "male");
            map2.put("age", "24");
    
            List<Map> list = new ArrayList<Map>();
            list.add(map1);
            list.add(map2);
    
            Employee employee = new Employee();
            employee.setName("ccc");
            employee.setSex("female");
            employee.setAge(25);
    
            // 将Map转换为JSONArray数据
            JSONArray ja1 = JSONArray.fromObject(map1);
            // 将List转换为JSONArray数据
            JSONArray ja2 = JSONArray.fromObject(list);
            // 将Bean转换为JSONArray数据
            JSONArray ja3 = JSONArray.fromObject(employee);
    
            System.out.println("JSONArray对象数据格式:");
            System.out.println("ja1:"+ja1.toString());
            System.out.println("ja2:"+ja2.toString());
            System.out.println("ja3:"+ja3.toString());
    
            // 构造Json数据,包括一个map和一个Employee对象
            jo.put("map", ja1);
            jo.put("employee", ja2);
            jo.put("xxx", ja3);
            System.out.println("
    最终构造的JSON数据格式:");
            System.out.println(jo.toString());
    
            return jo.toString();
    
        }
    
        /**
         * 解析Json数据
         * 
         * @param jsonString Json数据字符串
         */
        public static void ParseJson(String jsonString) {
    
            // 以employee为例解析,map类似
            JSONObject jb = JSONObject.fromObject(jsonString);
            JSONArray ja = jb.getJSONArray("employee");
    
            List<Employee> empList = new ArrayList<Employee>();
    
            // 循环添加Employee对象(可能有多个)
            for (int i = 0; i < ja.size(); i++) {
                Employee employee = new Employee();
    
                employee.setName(ja.getJSONObject(i).getString("name"));
                employee.setSex(ja.getJSONObject(i).getString("sex"));
                employee.setAge(ja.getJSONObject(i).getInt("age"));
    
                empList.add(employee);
            }
    
            System.out.println("
    将Json数据转换为Employee对象:");
            for (int i = 0; i < empList.size(); i++) {
                Employee emp = empList.get(i);
                System.out.println("name: " + emp.getName() + " sex: "
                        + emp.getSex() + " age: " + emp.getAge());
            }
    
        }
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
            ParseJson(BuildJson());
        }
    
    }

    json

    不存在json数组,单个json解析

            String jSting = "{  "code": 0,  "engDesc": null,  "chnDesc": null,  "detail": null,  "data": {    "sendCode": "08554401"  }}";
            // jSting = "[{"sendCode":"08554401"}]";
             JSONObject respObject = JSONObject.parseObject(jSting);
             String respCode = respObject.getString("code");
             JSONObject dataObject = respObject.getJSONObject("data");
             String sendCode = dataObject.getString("sendCode");
  • 相关阅读:
    树莓派frp添加为服务管理
    liunx开源打印驱动foo2zjs编译小坑
    树莓派中实现ll命令
    Windows中使用QEMU创建树莓派虚拟机
    C#打印条码BarTender SDK打印之路和离开之路(web平凡之路)(转)
    数据库连接池问题 Max Pool Size
    C#时间
    XAF 如何从Excel复制多个单元格内容到GridView(收藏)
    C#日期处理(转) 太忘记了,备忘
    C#、devExpress 的 给bandedGrid加菜单功能 :复制、粘贴的例子(转)
  • 原文地址:https://www.cnblogs.com/testway/p/5476445.html
Copyright © 2011-2022 走看看