标签:get rewrite ons obj data tco jsonp object tcl
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; } }
java fastjson:Map与json以及JSONObject ,JSONObject与String互转
标签:get rewrite ons obj data tco jsonp object tcl
原文地址:https://www.cnblogs.com/SunshineKimi/p/12233305.html