博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fastJson工具类
阅读量:4653 次
发布时间:2019-06-09

本文共 2851 字,大约阅读时间需要 9 分钟。

jar:fast.jar

依赖:

com.alibaba
fastjson
1.2.46

 

工具类

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 deserial(String str, Class
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"));		}	}}

  

 

转载于:https://www.cnblogs.com/lichangyunnianxue/p/9933649.html

你可能感兴趣的文章
[jQuery Note]jQuery Event
查看>>
java在不存在文件夹的目录下创建文件
查看>>
ideal项目启动及问题
查看>>
EasyUI学习笔记
查看>>
消除头文件
查看>>
InnoDB 锁
查看>>
回调函数
查看>>
无奈从Evernote International转向印象笔记
查看>>
JavaXML整理
查看>>
luogu P2312 解方程
查看>>
重新注册IIS
查看>>
第三周编程总结
查看>>
Leetcode: Remove Duplicate Letters
查看>>
Android中数据文件解析(Json解析)
查看>>
自定义seekBar设置进度条背景图片
查看>>
什么是SAP GUI的client
查看>>
Java面试题-线程安全
查看>>
java容器类1:Collection,List,ArrayList,LinkedList深入解读
查看>>
模块的导入顺序细节
查看>>
POJ 3255 Roadblocks
查看>>