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互转的更多相关文章

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

    [JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...

  2. JAVA array,map 转 json 字符串

    public class User { private String username; private String password; public String getUsername() { ...

  3. fastjson map转json

    Map map = new HashMap(); map.put("name", "老三"); map.put("age", 12); St ...

  4. java把map转json

    JSONUtils.toJSONString(requestMap);    com.alibaba.fastjson.JSON <!-- https://mvnrepository.com/a ...

  5. java 字符串解析为json 使用org.json包的JSONObject+JSONArray

    参考: https://blog.csdn.net/xingfei_work/article/details/76572550 java中四种json解析方式 JSONObject+JSONArray ...

  6. java map转json servlet response

    1.手写一个map转json的类 1.1 调用方式 //给前端放回json数据 Map<String, Object> map = new HashMap<>(); map.p ...

  7. java List<Map> 排序问题

    Collections.sort(order_from_list, new Comparator<Map<Object, Object>>() { public int com ...

  8. java的map取值

    第一种方法根据键值的名字取值 import java.util.HashMap; import java.util.Map; /**   * @param args   */  public stat ...

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

    JSON与JAVA数据的转换(JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.) 上一篇文章中有这么 ...

随机推荐

  1. 【python基础语法】数据类型:数值、字符串 (第2天课堂笔记)

    """ 数据类型: 一.数值类型:整数 浮点数 布尔值 二.序列类型:字符串.列表 元祖 三.散列类型:字典 集合 可变数据类型: 列表 字典 集合,可以改动内存地址数据 ...

  2. java-标准输入

    package InputDemo; public class InputDemo1 { public static void main(String[] args) { /* 1. java.uti ...

  3. Android记事本在菜单栏添加搜索按钮方法

    效果图 这个app结构和我之前将记事本开发的博客基本一致,我这里直接讲一下怎样添加 使用的开发软件为android studio 首先在res目录下新建文件夹menu,添加目录布局文件main_men ...

  4. 2020 CCPC Wannafly Winter Camp Day1 Div.1&amp F

    #include<bits/stdc++.h> #define forn(i, n) for (int i = 0; i < int(n); i++) #define fore(i, ...

  5. [PAT] A1018 Public Bike Management

    [思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...

  6. 安全 - CORS(脚本请求等)

    功能概述 出于安全原因,浏览器限制从脚本内发起的跨域HTTP请求 或 拦截了跨域请求的结果. 例如,XMLHttpRequest和Fetch API遵循同源策略. 这意味着使用这些API的Web应用程 ...

  7. 第二章 Spring MVC入门 —— 跟开涛学SpringMVC 读后感1

    2.1.Spring Web MVC是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职 ...

  8. 第十周 11.28 psp0

    课堂测试总结 学生:马小心                                              日期:2017.11.28 作业号 日期 过程 估计数据 实际数据 累计数据 时间 ...

  9. 跨站脚本(XSS)

    1.1 XSS定义 XSS,即为(Cross Site Scripting),中文名为跨站脚本,是发生在目标用户的浏览器层面上的,当渲染DOM树的过程发生了不在预期内执行的JS代码时,就发生了XSS攻 ...

  10. Ionic 使用 NFC

    Ionic 使用 NFC 哎哟喂,因为项目需要使用 Ionic 调用手机 NFC 功能,踩了好多坑,真的是,不过终于不负众望拿到了id.现在就记录一下我的步骤和踩过的坑! 步骤 我装的Ionic可能是 ...