• 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;
        }
    
    
    }
    

      

  • 相关阅读:
    mysql 定时器
    mysql 存储过程记录
    mysql 常用sql
    mysql 取最后一条数据 分组
    Java8 Stream使用flatMap合并List 交 并 合 差集
    微服务技术栈
    spring boot 拦截 以及Filter和interceptor 、Aspect区别
    在springMVC的controller中获取request,response对象的一个方法
    spring boot 用@CONFIGURATIONPROPERTIES 和 @Configuration两种方法读取配置文件
    SSRS 2016 Forms Authentication
  • 原文地址:https://www.cnblogs.com/SunshineKimi/p/12233305.html
Copyright © 2020-2023  润新知