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. C语言 if

    C语言 if C语言支持最基本的三种程序运行结构:顺序结构.选择结构.循环结构. 顺序结构:程序按顺序执行,不发生跳转. 选择结构:依据是否满足条件,有选择的执行相应功能. 循环结构:依据条件是否满足 ...

  2. final关键字在JVM中的理解

    我们先来看两段代码与运行结果,根据结果去分析解释一下 不加final关键字: package com.waibizi; public class demo02 { public static void ...

  3. VMware、Linux(CentOS 7)安装,供参考。

    前端开发,学习Linux,记录安装中的一些问题,比如在安装最小版后,如何由命令行转为图形界面等. -------------笔记中截图参考其他资料. 下载文件 VMware 12: 链接:https: ...

  4. 剑指offer-面试题17-打印从1到最大的n位数-数字

    /* 题目: 输入数字n,按顺序打印从1到最大的n位十进制数. 如输入3,打印从1,2,3到999. */ /* 思路: 大数问题转化为字符串或数组. */ #include<iostream& ...

  5. cf1266D

    注意到每一个的点出入流是不会变的,因此本质是让构造一张图满足这个出入流并且边上的流量之和最少,显然流量是平衡的,也就是所有节点的出入流之和为0 因此我们可以直接暴力的选择让负数点向正数点连边,连之后就 ...

  6. Leetcode Week5 Maximum Sum Circular Subarray

    Question Given a circular array C of integers represented by A, find the maximum possible sum of a n ...

  7. 今天才知道a标签的href="#"是回到页面顶部

    如题,真的是,做了一年多的开发,今天才知道a标签的href="#"是回到顶部.以前一直以为这是个多么了不起的功能. 顺便扩展一下滑动隐藏和显示按钮(从别处拷贝来的代码) $( do ...

  8. Centos7 同时运行PHP5.2和PHP7.1配置

    工作环境一直都是lnmp,其中PHP已经升级到7.1版本了.突然有份代码需要运行在PHP5.2上.但是之前的环境还是需要的,所以需要在centos中再安装PHP5.2. 0.之前的php7安装在/us ...

  9. 原生js判断设备类型

    var u = navigator.userAgent; //Android终端 var isAndroid = u.indexOf('Android') > -1 || u.indexOf(' ...

  10. windows-problem :电脑上网的无线图标带有黄色星号,但不影响正常上网!

    电脑可以上网,但是无线图标带有黄色星号如何解决? 进入“网络和共享中心”,点击“更改适配器设置”,看看是不是有两个以上的“本地连接”,只有一个有用,其他的删除即可.