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

}

  

原文地址:https://www.cnblogs.com/SunshineKimi/p/12233305.html

时间: 2024-08-08 19:26:38

java fastjson:Map与json以及JSONObject ,JSONObject与String互转的相关文章

Java中将Map转换为JSON

一个注意的地方:要选对jar包 1 Map map = new HashMap(); 2 map.put("success", "true"); 3 map.put("photoList", photoList); 4 map.put("currentUser", "zhang"); 5 6 //net.sf.json.JSONObject 将Map转换为JSON方法 7 JSONObject json =

JS json对象(Object)和字符串(String)互转方法

[JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 string -> jsonObj JSON.parse(jsonString); jsonObj -> string JSON.stringify(jsArr); 记录一下 原文地址:https://www.cnblogs.com/d-i-p/p/11025164.html

JAVA array,map 转 json 字符串

Java代码   public class User { private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } pu

fastjson map转json

Map map = new HashMap(); map.put("name", "老三"); map.put("age", 12); String jsonStr = JSONObject.toJSONString(map); 然后return 原文地址:https://www.cnblogs.com/jnhs/p/10178655.html

采用json-lib进行Map与Json转换

Json转换工具实在之多,最近又听说FastJson对Java序列化和反序列化最优,相比 Java自带序列化.Json-lib.Jackson等.不过本人很青睐Gson,但是好像用的人也不是很多,项目中用的最多的就是垃圾Json-lib了,虽然烂,但是还是的继续使用着,因为项目在我来之前已经搭建了,不过现在我推荐使用Jackson,官网说到这是Json工具中最快的一个,当然是有一点吹牛的,因为他没有拿数据说话!之所以推荐Jackson,Jackson确实速度快,而且Spring内部原生支持Jac

java的map取值

第一种方法根据键值的名字取值 import java.util.HashMap; import java.util.Map; /**   * @param args   */  public static void main(String[] args) {   // TODO Auto-generated method stub         Map<String, Object> map=new HashMap<String, Object>();         map.p

JSONObject.fromObject(map)(JSON与JAVA数据的转换)

JSON与JAVA数据的转换(JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.) 上一篇文章中有这么一句,是后台的封装数据. JSONObject jo = JSONObject.fromObject(map);常见的java代码转换成json 1. List集合转换成json代码 List list = new ArrayList(); list.add( "first" ); lis

Fastjson, Gson, org.json.JSON三者对于JSONObject及JSONArray的判断

1.Fastjson 我们通常在已知格式的情况下直接使用JSONObject,JSONArray,但是如果遇到需要判断格式呢? try{ Object object = JSON.parse(a); if (object instanceof JSONObject){ //JSONObject } if (object instanceof JSONArray){ //JSONArray } }catch (com.alibaba.fastjson.JSONException e){ //非JS

Java生成json串、JSONObject与JSONArray的使用

1.JAR包简介 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar 2.JSONObject对象使用 JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转