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工具类的更多相关文章

  1. Gson/Jackson/FastJson工具类

    import java.util.ArrayList; import java.util.List; import java.util.Map; import com.google.gson.Gson ...

  2. 阿里fastjson工具类

    package com.common.utils.jsonUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JS ...

  3. 工具类-Fastjson入门使用

    简介 什么是Fastjson? fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到Java ...

  4. FastJsonUtils工具类

    fastjson是由alibaba开源的一套json处理器.与其他json处理器(如Gson,Jackson等)和其他的Java对象序列化反序列化方式相比,有比较明显的性能优势. 版权声明:本文为博主 ...

  5. Java常用工具类---image图片处理工具类、Json工具类

    package com.jarvis.base.util; import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStre ...

  6. Json转换工具类(基于google的Gson和阿里的fastjson)

    在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...

  7. Redis在JAVA中的运用(工具类)

    最近项目需要用redis在中间做缓存所以写了一个工具类作为练习用 redis版本:redis_version:3.0.504 用到阿里的解析JSON的库:fastjson import org.apa ...

  8. SharePreference 工具类封装

    import java.util.List;import java.util.Map;import java.util.Set;import com.alibaba.fastjson.JSON;imp ...

  9. IP工具类-自己动手做个ip解析器

    IP工具类-自己动手做个ip解析器 一.资料准备 导入依赖包:

随机推荐

  1. CF277B Set of Points——构造题

    题意 构造 $n$ 个点使其凸度(顶点数最多的凸多边形的顶点数)恰好为 $m$,且任意三点不能共线. (题意有点绕,建议看英文原文 分析 首先,题目并不是求凸包上的顶点数,而是求能形成的凸多边形的最大 ...

  2. 数位DP【模板】

    经典题型 数位 DP 问题往往都是这样的题型,给定一个闭区间 $[l, r]$,让你求这个区间中满足 某种条件 的数的总数. 强烈推荐 OI Wiki——数位DP 例题 BZOJ 1026 题目:wi ...

  3. BZOJ1209 最佳包裹 (三维凸包 增量法)

    题意 求三维凸包的表面积. N≤100N\le100N≤100 题解 暴力往当前的凸包里加点.O(n2)O(n^2)O(n2).题解详见大佬博客 扰动函数shakeshakeshake是为了避免四点共 ...

  4. js原型模式和继承

    function SuperType() { this.property = true; //原型属性 } //基类方法 SuperType.prototype.getSuperValue = fun ...

  5. 通过自定义属性获取指定checkbox是否选中

    $("input[conferid='"+conferid+"']").is(':checked'); $("input[conferid='1234 ...

  6. Postgresql vacuum freeze相关参数

    先看3个参数:autovacuum_freeze_max_age           | 500000vacuum_freeze_min_age               | 10vacuum_fr ...

  7. MyBatis中jdbcType=INTEGER、VARCHAR作用

    Mapper.xml中 pid = #{pid,jdbcType=INTEGER} pid = #{pid} 都可以用 Mybatis中什么时候应该声明jdbcType? 当Mybatis不能自动识别 ...

  8. zookpeer 和 redis 集群内一致性协议 及 选举 对比

    zookeeper 使用的是zab协议,类似 raft 的 Strong Leader 模式 redis 的哨兵 在  崩溃选举的时候采用的是 raft的那一套term. 因为redis 采用的是异步 ...

  9. kafka 讲讲acks参数对消息持久化的影响

    目录 (0)写在前面 (1)如何保证宕机时数据不丢失? (2)多副本冗余的高可用机制 (3)多副本之间数据如何同步? (4)ISR到底指的什么东西? (5)acks参数的含义? (6)最后的思考   ...

  10. [Andorid] 通过JNI实现kernel与app进行spi通讯

    CPU:RK3399 系统:Android 7.1 人脸识别的要求越来越高,因此主板增加了 SE 加密芯片,加密芯片通过 spi 接口与 CPU 通讯. 对于 kernel 层的代码,Linux 原始 ...