zoukankan      html  css  js  c++  java
  • java fastjson:Map与json以及JSONObject ,JSONObject与String互转

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject ;
    import com.alibaba.fastjson.JSONPath;
    import com.jayway.jsonpath.Configuration;
    import com.jayway.jsonpath.JsonPath;
    
    
    import java.util.Map;
    
    
    public class fastTestJson {
    
        static void type(Object o){
            print(o.getClass().getName());
        }
    
        public static void main(String[] args) {
            String obj = "{"data":{"access_token":"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206","expires_second":36000},"rlt_code":"HH0000","rlt_msg":"成功"}";
            ;
            JSONObject JS = JSONObject.parseObject(obj);
            getJsonValue(JS);
            // test jsonArray
            String test = "{"success":true,"data":[{"building_id":"***","building_num":"**","room_name":"**","door_name":"**","electric":"**"}]}";
            JSONObject jsonObject = JSON.parseObject(test);
            print(jsonObject);
            JSONArray data = jsonObject.getJSONArray("data");
            print(data);
            JSONArray jsonArray = (JSONArray) JSONPath.read(jsonObject.toJSONString(), "$.data.room_name");
            Object o = jsonArray.get(0);
            type(o);
        }
    
        /** rewrite sys.out */
        static void print(Object text) {
            System.out.println(text);
        }
    
        /**  字符串string  转 map */
        static Map StringTOMap(String jsonStr){
            return  (Map)JSON.parse(jsonStr);
        }
    
        /** map 转 string  by fastjson   */
        static String mapToString (Map  map ){
            return JSON.toJSONString(map);
        }
    
    
        /** json 对象转map  */
        static Map JSONObjectToMap(JSONObject o){
            // jsonObject.toString()
            Map  params = JSON.parseObject(o.toString());
            return params;
        }
        /**  map 转 json对象 */
        static JSONObject mapToJson(Map map){
    
            JSONObject jsonObject  = new JSONObject(map);
            return  jsonObject;
        }
    
        /** 字符串 转都JSONObject对象*/
        static JSONObject jsonObject (String jsonstr){
            return JSONObject.parseObject(jsonstr);
        }
        /** json对象转字符串 str*/
        static  String  getStr(JSONObject jsonObject){
            return jsonObject.toJSONString();
        }
    
        static void   getJsonValue(JSONObject jsonObject){
            /** {"rlt_code":"HH0000","data":{"access_token":"5a7040ccf66bafd06acd39b6f61c19230eaba426755509646d6da23ddd9fb206",
             * "expires_second":36000},"rlt_msg":"成功"} */
    
            String data= jsonObject.getString("data");  //get String value of json object
            print(data);
            JSONObject js = JSONObject.parseObject(data);
    
        }
    
        static Object  jsonPathRead(String json){
            // to get  just one time read json object .
            Object document = Configuration.defaultConfiguration().jsonProvider().parse(json);
            String context1 = JsonPath.read(document,"$..name");
            JSONArray context2 =JsonPath.read(document,"$..data");
            return  context1;
        }
    
    
    }
    

      

  • 相关阅读:
    妙味——自定义滚动条
    妙味——拖拽改变大小
    妙味——带框的拖拽
    IE6 固定定位
    JavaScript 事件绑定
    JavaScript 事件
    设置指定网页为主页
    [LeetCode][JavaScript]Compare Version Numbers
    [LeetCode][JavaScript]Implement Stack using Queues
    [LeetCode][JavaScript]Invert Binary Tree
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12233305.html
Copyright © 2011-2022 走看看