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. UVALive-7040-Color(容斥原理)

    链接: https://vjudge.net/problem/UVALive-7040 题意: Recently, Mr. Big recieved n owers from his fans. He ...

  2. VMWare虚拟机中网络连接类型对比

    1.NAT NAT:Network Address Translation,网络地址转换:虚拟机的网卡连接到宿主的 VMnet8 上 虚拟机与主机的关系:只能单向访问,虚拟机可以通过网络访问到主机,主 ...

  3. hive基础及系统架构

    1.hive是什么 hive是建立在hadoop上的数据仓库,提供数据的提取.转化和加载. 2.hive的数据存储 1]hive的数据存储基于hdfs 2]存储结构主要包括:数据库.文件.表.索引.视 ...

  4. PCIe - 周扒皮,扒扒TLP层

    来来来,南来的,北往的,看一看,瞧一瞧,不好用不要钱,快来看呐. 来,客观,摘抄几篇PCIe文章给您看看, 1. http://xillybus.com/tutorials/pci-express-d ...

  5. 数据结构实验之图论七:驴友计划【迪杰斯特拉算法】(SDUT 3363)

    分析:可以求简单的任意两点间最短距离的稍微变形,一个板子题.  #include <iostream> #include <bits/stdc++.h> using names ...

  6. 支持utf8的str_split函数

    <?php header("Content-type: text/html; charset=utf-8"); /** * 按字节数对字符串进行分片 * @param $st ...

  7. [WEB安全]代码/命令执行总结

    0x01 代码执行 1.1 概念 远程代码执行实际上是调用服务器网站代码进行执行. 1.2 常见执行方法 eval eval():将字符串当做函数进行执行(需要传入一个完整的语句) demo: < ...

  8. AT2306 Rearranging

    有一个显然的,就是不互质的数的相对位置是不会改变的,那么我们把它们放到一个连通块里面去,然后我交换就是交换两个里面最小的对吧.直接连起来然后跑\(TopSort\)就行了. #include<s ...

  9. mysql的select语句

    参考: https://www.cnblogs.com/xiaoshen666/p/10824117.html https://www.cnblogs.com/zouwangblog/archive/ ...

  10. centos6和centos7中常用命令区别

    以前一直接触的是centos6,最近因为新项目接触到centos7,发现有些命令还是有差异的(从centos7开始使用systemctl来管理服务和程序,包括了service和chkconfig),现 ...