zoukankan      html  css  js  c++  java
  • JSONObjectSample

    package com.egeniuss.platform.basic;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    public class JSONObjectSample {
        /**
         * 创建JSONObject对象 入参map JSON节点为map.key jSON节点值为map.get(key)
         **/
    
        public static JSONObject createJSONObject(Map reJsonMap) {
            JSONObject jsonObject = new JSONObject();
            if (reJsonMap != null && reJsonMap.size() > 0) {
                Set keySet = reJsonMap.keySet();
                Iterator ite = keySet.iterator();
                String name = "";
                while (ite.hasNext()) {
                    name = (String) ite.next();
                    System.out.println("key:" + name);
                    jsonObject.put(name, reJsonMap.get(name));
                }
            }
            return jsonObject;
        }
    
        // 创建JSONObject对象
        public JSONObject createJSONObject(Object reJson) {
            JSONObject jsonObject = JSONObject.fromObject(reJson);
            return jsonObject;
        }
    
        /**
         * 解析JSONObject对象 入参:string(jSON格式),属性名称 出参 String
         */
        public String decodeJSONObject(String resJson, String name) {
            String nameValue = "";
            JSONObject reqObj = JSONObject.fromObject(resJson);
            nameValue = reqObj.getString(name);
            nameValue = nameValue == null ? "" : nameValue;
            return nameValue;
        }
    
        /**
         * 解析JSONObject对象 入参:JSONObject,属性名称 出参 String
         */
        public String decodeJSONObject(JSONObject resJson, String name) {
            String nameValue = "";
            nameValue = resJson.getString(name);
            nameValue = nameValue == null ? "" : nameValue;
            return nameValue;
        }
    
        /**
         * 解析JSONObjectArray对象 入参:JSONObject,属性名称 出参 String
         */
        public List decodeJSONArray(String resJson, String name) {
            List list = new ArrayList();
            JSONObjectSample js = new JSONObjectSample();
            JSONArray jsonArr = JSONArray.fromObject(js.decodeJSONObject(resJson,
                    name));
            for (int i = 0; i < jsonArr.size(); i++) {
                String[] array = new String[2];
                array[0] = js.decodeJSONObject(jsonArr.getJSONObject(i).toString(),
                        "createTime");
                array[1] = js.decodeJSONObject(jsonArr.getJSONObject(i).toString(),
                        "description");
                System.out.println("bpm=" + array[0] + "dpm" + array[1]);
            }
            return list;
        }
    
        public static void main(String[] args) {
            // 输出jsonobject对象
            Map map = new HashMap();
            map.put("aaaaaa", "awdqweq");
            map.put("bbbb", "awdqweq");
            map.put("ccc", "awdqweq");
            map.put("fdd", "awdqweq");
            map.put("aaxxxaaaa", "awdqweq");
            map.put("aaaaewqeaa", "awdqweq");
            map.put("aaaadawdaa", "awdqweq");
            map.put("eweq", "awdqweq");
            JSONObject jsonObject = JSONObjectSample.createJSONObject(map);
            System.out.println("map的长度" + map.size());
            System.out.println("jsonObject==>" + jsonObject);
            // 判读输出对象的类型
            boolean isArray = jsonObject.isArray();
            boolean isEmpty = jsonObject.isEmpty();
            boolean isNullObject = jsonObject.isNullObject();
            System.out.println("isArray:" + isArray + " isEmpty:" + isEmpty
                    + " isNullObject:" + isNullObject);
            //
            // //添加属性
            jsonObject.element("address", "swap lake");
            System.out.println("添加属性后的对象==>" + jsonObject);
    
            // 返回一个JSONArray对象
            String a = "{"resultCode":"0","msg":"","data":[{"createTime":"2014-02-22 19:13:27","description":"理赔受理"}]}";
            JSONObjectSample js = new JSONObjectSample();
            // a = js.decodeJSONObject(a, "data");
            // System.out.println(a);
            // jsonObject.decodeJSONObject(a,"resultCode");
            JSONArray jsonArray = new JSONArray();
            jsonArray.add(0, "this is a jsonArray value");
            jsonArray.add(1, "another jsonArray value");
            jsonObject.element("jsonArray", jsonArray);
            // jsonArray=jsonObject.getJSONObject(a);
            // JSONArray jsonArr = JSONArray.fromObject(a);
            // String s= array.getString(1);
            // for (int i = 0; i < jsonArr.size(); i++)
            // {
            // String dpm= js.decodeJSONObject(jsonArr.getJSONObject(i).toString(),
            // "description");
            // System.out.println("bpm="+bpm+"dpm"+dpm);
            // }
            js.decodeJSONArray(a, "data");
            // String json =
            // "[{"a":"111","b":"222","c":"333"},{"a":"1000","b":"2000","c":"000"},{"a":"999","b":"300","c":"700"}]";
            // JSONArray jsonArr = JSONArray.fromObject(json);
            // String a1[] = new String[jsonArr.size()];
            // String b[] = new String[jsonArr.size()];
            // String c[] = new String[jsonArr.size()];
            // for (int i = 0; i < jsonArr.size(); i++) {
            // a1[i] = jsonArr.getJSONObject(i).getString("a");
            // b[i] = jsonArr.getJSONObject(i).getString("b");
            // c[i] = jsonArr.getJSONObject(i).getString("c");
            // }
            //
            // for (int i = 0; i < c.length; i++) {
            // System.out.print(a1[i]+" ");
            // System.out.print(b[i]+" ");
            // System.out.print(c[i]);
            // System.out.println();
            // }
            // //添加JSONArray后的值
            // {"resultCode":"0","msg":"","data":[{"createTime":"2014-02-22 19:13:27","description":"理赔受理"}]}
            // {"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
            // "jsonArray":["this is a jsonArray value","another jsonArray value"]}
            System.out.println(jsonObject);
            //
            // //根据key返回一个字符串
            // String jsonString = jsonObject.getString("name");
            // System.out.println("jsonString==>"+jsonString);
            //
            // 根据key返回一个字符串
            // String decodeString = JSONObjectSample.decodeJSONObject(jsonObject,
            // "eweq");
            // System.out.println("decodeString==>"+decodeString);
            // RespContext respContext = new RespContext();
            // respContext.setAlipayUserId("dawweq");
            // respContext.setComId("weqwe2312");
            // JSONObject jsonObjec1t =
            // JSONObjectSample.createJSONObject(respContext);
            System.out.println("jsonObjec1t==>" + jsonObject);
    
        }
    
    }
  • 相关阅读:
    Linux——k8s命令别名修改
    k8s—centos7安装部署NFS服务器和客户端及基于nfs的动态存储storageclass使用总结
    MySQL—用户和权限管控
    MySQL—常用SQL语句整理总结
    Zookeeper——入门介绍(相关原理、安装启动及使用操作)
    SpringBoot——Quartz定时框架的使用详解和总结
    SpringBoot——@Scheduled的自定义周期性线程池解决任务延时执行问题
    Linux—用户新建目录和文件的默认权限设置:umask详解
    设计模式——单例模式详解
    Linux—CPU核数、上下文切换介绍及pidstat等命令详解
  • 原文地址:https://www.cnblogs.com/mengyuxin/p/5803969.html
Copyright © 2011-2022 走看看