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;
} }
java fastjson:Map与json以及JSONObject ,JSONObject与String互转的更多相关文章
- JS json对象(Object)和字符串(String)互转方法
[JS json对象(Object)和字符串(String)互转方法] 参考:https://blog.csdn.net/wenqianla2550/article/details/78232706 ...
- JAVA array,map 转 json 字符串
public class User { private String username; private String password; public String getUsername() { ...
- fastjson map转json
Map map = new HashMap(); map.put("name", "老三"); map.put("age", 12); St ...
- java把map转json
JSONUtils.toJSONString(requestMap); com.alibaba.fastjson.JSON <!-- https://mvnrepository.com/a ...
- java 字符串解析为json 使用org.json包的JSONObject+JSONArray
参考: https://blog.csdn.net/xingfei_work/article/details/76572550 java中四种json解析方式 JSONObject+JSONArray ...
- java map转json servlet response
1.手写一个map转json的类 1.1 调用方式 //给前端放回json数据 Map<String, Object> map = new HashMap<>(); map.p ...
- java List<Map> 排序问题
Collections.sort(order_from_list, new Comparator<Map<Object, Object>>() { public int com ...
- java的map取值
第一种方法根据键值的名字取值 import java.util.HashMap; import java.util.Map; /** * @param args */ public stat ...
- JSONObject.fromObject(map)(JSON与JAVA数据的转换)
JSON与JAVA数据的转换(JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,非常适合于服务器与 JavaScript 的交互.) 上一篇文章中有这么 ...
随机推荐
- [SDOI2012] 任务安排 题解
有感而发,遂书. 其实和sze聊了很久,但他还是退役了.恐怕他是本届里学oi时间最长的一个人吧,从小学五年级开始.我也是因为他,才开始学oi的.他因为学校的压力,不得不放弃.或许是没什么天赋.学了4年 ...
- SpringBoot导出excel数据报错Could not find acceptable representation
转自:https://blog.csdn.net/mate_ge/article/details/93518286?utm_source=distribute.pc_relevant.none-tas ...
- babel环境安装与编译
babel:将浏览器不支持的ES6语法转为javascript 查看node是否安装: npm -v node -v 实例演示:在桌面新建part5目录在cmd命令行中 cd desktop cd p ...
- ng-http
启用 Http 服务 open the root AppModule, import the HttpClientModule symbol from @angular/common/http, ad ...
- python的优先级
在编写程序时,我遇到麻烦!怎么找都找不到bug 最终我发现了是我搞错了运算符优先级 位运算要在加减后面(这可真奇怪) eg 10-10^11=11!!! 还是多加括号的好
- linux100讲——71 if-else判断的使用
1.if-then-else语句: 语法: if [测试条件成立] then 执行相应的命令 else 测试条件不成立,执行相应的命令 fi 结束 示例:vim 9.sh #!/bin/bash #i ...
- Web APIs---2. DOM(1)
1 DOM简介 1.1 定义 DOM就是文档对象模型,是W3C组织推荐的处理可扩展标记语言(HTML或者XML)的标准编程接口.W3C已经定义了一系列的DOM接口,通过这些DOM接口可以改变网页的内容 ...
- Java锁机制深入理解
Java锁机制 背景知识 指令流水线 CPU的基本工作是执行存储的指令序列,即程序.程序的执行过程实际上是不断地取出指令.分析指令.执行指令的过程. 几乎所有的冯•诺伊曼型计算机的CPU,其工 ...
- Codeforces667D(spfa+dp)
题意: 给定一个带权有向图,若P(A,B)表示节点A到B的最短路长度,选择四个节点ABCD,使得P(A,B)+P(B,C)+P(C,D)最大. 节点数n在1,000以内,边数m在2,000以内. 思路 ...
- ubuntu 16.04.1上安装并使用vsftpd
1.安装vsftpd软件 sudo apt-get install vsftpd 2.新建文件 sudo vim /etc/vsftpd.user_list 用于记录用户名 3. 修改sudo vim ...