fastJson工具类
jar:fast.jar
依赖:
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
工具类
package json; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import json.test.Student; public class JSONSerial { public static JSONArray toJsonArray(String str) {
JSONArray jsonArray = JSON.parseArray(str);
return jsonArray;
} public static JSONObject toJson(String str) {
JSONObject jsonObject = JSON.parseObject(str);
return jsonObject;
} public static String serial(Object obj) {
return JSON.toJSONString(obj);
} public static <T> T deserial(String str, Class<T> clazz) {
if (str == null || str.length() == 0) {
return null;
}
return JSON.parseObject(str, clazz);
} public static void main(String[] args) {
String jsonStr = "{\"studentName\":\"lily\",\"studentAge\":12}";
// JSONObject j = toJson(jsonStr);
// System.out.println(j.getString("studentName")); Student s = deserial(jsonStr, Student.class);
System.out.println(s.getStudentAge());
String strs = serial(s);
System.out.println(strs); String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
JSONArray jsonArray = toJsonArray(JSON_ARRAY_STR);
for(Object obj : jsonArray){
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.get("studentName"));
}
}
}
测试类
package json.test; public class Student {
private String studentName;
private int studentAge;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
} }
测试执行类
package json.test; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import json.JSONSerial; /**
* @Title: TestFastJson.java
* @Package json.test
* @Description: TODO(用一句话描述该文件做什么)
* @author licy
* @date 2018年11月9日
* @version V1.0
*/ public class TestFastJson {
public static void main(String[] args) {
String jsonStr = "{\"studentName\":\"lily\",\"studentAge\":12}";
// JSONObject j = toJson(jsonStr);
// System.out.println(j.getString("studentName")); Student s = JSONSerial.deserial(jsonStr, Student.class);
System.out.println(s.getStudentAge());
String strs = JSONSerial.serial(s);
System.out.println(strs); String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
JSONArray jsonArray = JSONSerial.toJsonArray(JSON_ARRAY_STR);
for(Object obj : jsonArray){
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.get("studentName"));
}
}
}
fastJson工具类的更多相关文章
- Gson/Jackson/FastJson工具类
import java.util.ArrayList; import java.util.List; import java.util.Map; import com.google.gson.Gson ...
- 阿里fastjson工具类
package com.common.utils.jsonUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JS ...
- 工具类-Fastjson入门使用
简介 什么是Fastjson? fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到Java ...
- FastJsonUtils工具类
fastjson是由alibaba开源的一套json处理器.与其他json处理器(如Gson,Jackson等)和其他的Java对象序列化反序列化方式相比,有比较明显的性能优势. 版权声明:本文为博主 ...
- Java常用工具类---image图片处理工具类、Json工具类
package com.jarvis.base.util; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStre ...
- Json转换工具类(基于google的Gson和阿里的fastjson)
在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...
- Redis在JAVA中的运用(工具类)
最近项目需要用redis在中间做缓存所以写了一个工具类作为练习用 redis版本:redis_version:3.0.504 用到阿里的解析JSON的库:fastjson import org.apa ...
- SharePreference 工具类封装
import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;imp ...
- IP工具类-自己动手做个ip解析器
IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:
随机推荐
- sql server 综合使用的例子
exec sp_helptext prosampleoldstyle_usp -- ============================================= -- ========= ...
- SpringMVC使用ResponseEntity实现文件下载,及图片base64的字节数组上传于下载
本文主要通过ResponseEntity<byte[]>实现文件下 该类实现响应头.文件数据(以字节存储).状态封装在一起交给浏览器处理以实现浏览器的文件下载. ResponseEntit ...
- SQL Server查询性能
sql server常用语句总结 http://ace105.blog.51cto.com/639741/792519 SQL Server 性能调优(一)--从等待状态判断系统资源瓶颈 ...
- NetworkX系列教程(3)-手动创建graph
小书匠Graph图论 不可否认,日常中我们使用最多的还是,使用自己的数据去手动创建自己的图形,而不是使用生成器,现从给graph添加点和边入手,讲解手动创建graph. 目录: 3.给graph添加节 ...
- SSD检测几个小细节
目录 一. 抛砖引玉的Faster-RCNN 1.1 候选框的作用 1.2 下采样问题 二. SSD细节理解 2.1 六个LOSS 2.2 Anchor生成细节 2.3 Encode&& ...
- SLAM第一篇:基础知识
无论在室内.野外.空中还是水下,SLAM是机器人进入未知环境遇到的第一个问题.本期给大家介绍SLAM的基础知识:传感器与视觉SLAM框架 近来年,智能机器人技术在世界范围内得到了大力发展.人们致力于把 ...
- Linux下基于Xampp的TestLink的安装部署
由于项目需要,故需要搭建Testlink,且出于稳定,考虑在linux环境部署,当然windows系统也是可以的. 系统:64位操作系统 linux:centos6.8 testlink:1.9.14 ...
- spoj Longest Common Substring (多串求最大公共子序列)
题目链接: https://vjudge.net/problem/SPOJ-LCS 题意: 最多10行字符串 求最大公共子序列 数据范围: $1\leq |S| \leq100000$ 分析: 让他们 ...
- GO windows下编译luajit
1 GO嵌入luajit需要用到cgo,使用cgo需要安装gcc,在windows上下载MinGW-W64安装上配置好环境变量就可以 2 gcc编译luajit,生成.a文件. 把LuaJIT-2.0 ...
- mysql全面优化
在进行MySQL的优化之前,必须要了解的就是MySQL的查询过程,很多查询优化工作实际上就是遵循一些原则,让MySQL的优化器能够按照预想的合理方式运行而已. 图-MySQL查询过程 一.优化的哲学 ...