zoukankan      html  css  js  c++  java
  • JSON基本操作

    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    
    
    
    public class test1 {
    
        /**
         * @param <JSONObject>
         * @param args
         */
        public static  void main(String[] args) {
            // TODO Auto-generated method stub
            //将json对象转换为字符串
            JSONObject obj=new JSONObject();
            obj.put("ooo", "***");
            obj.put("ppp", "###");
            JSONObject object=new JSONObject();
            object.put("aaa", "123");
            object.put("bbb", "456");
            object.put("ccc", obj);
            System.out.println(object);
            
            //将json数组对象转换为字符串
            JSONArray  arry=new JSONArray();
            JSONObject object1=new JSONObject();
            object1.put("aaa", "123");
            object1.put("bbb", "456");
            JSONObject object2=new JSONObject();
            object2.put("ccc", "123");
            object2.put("ddd", "456");
            arry.add(object1);
            arry.add(object2);
            System.out.println(arry.toJSONString());
            
            
            //将字符串转换为JSON对象
            String strJson ="{"aaa":"111","bbb":"222","ccc":"333","ddd":"444"}";
            JSONParser parser=new JSONParser();
            try {
                JSONObject onject3=(JSONObject) parser.parse(strJson);
                System.out.println(onject3.toJSONString());
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            //将字符串转换为JSON数组对象
             String jsonStr1 = "[{"aaa":"111","bbb":"222"},{"aaa":"111","bbb":"222"}]";  
                try {  
                    JSONArray parseObject = (JSONArray)parser.parse(jsonStr1);  
                    System.out.println( parseObject.toJSONString());  
                } catch (ParseException e) {  
                    e.printStackTrace();  
                }  
            
        }
    
    }
    View Code
  • 相关阅读:
    SQLQuery实现动态表映射
    Hibernate Criteria查询
    关于短延迟 SLEEP USLEEP NANOSLEEP SELECT
    FFmpeg技术资料
    container_of()
    AES加解密算法的模式介绍
    无线AES与TKIP
    妻子1.0
    VLC简介及使用说明
    虚拟机中BusLogic与LSILogic的区别与分析
  • 原文地址:https://www.cnblogs.com/feitianshaoxai/p/6000790.html
Copyright © 2011-2022 走看看