Alibaba 原创FastJson工具

前端之家收集整理的这篇文章主要介绍了Alibaba 原创FastJson工具前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.alibaba.json;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;

public class JsonAndObj {
	public static void main(String[] args) {
		Student student = new Student("刘XX",21);
		Student student1 = new Student("大XX",21);
		// JAVA对象
		String jsonStr = JSON.toJSONString(student);
		System.out.println(jsonStr);

		// 把jsonStr转回java对象
		Student s = JSON.parSEObject(jsonStr,Student.class);
		System.out.println(s.getName());
		System.out.println(s.getAge());

		// List集合(JavaBean)
		List<Student> list = new ArrayList<Student>();
		list.add(student);
		list.add(student1);

		jsonStr = JSON.toJSONString(list);
		System.out.println(jsonStr);

		// 把list(JavaBean)的jsonStr转回List集合
		List<Student> listStu = JSON.parseArray(jsonStr,Student.class);
		for (Student stu : listStu) {
			System.out.println(stu.getName());
			System.out.println(stu.getAge());
		}

		// list<String>
		List<String> strings = new ArrayList<String>();
		strings.add(new String("aaa"));
		strings.add(new String("bbb"));
		strings.add(new String("ccc"));
		System.out.println(JSON.toJSON(strings));
		String a = JSON.toJSONString(strings);
		List<String> newa = JSON.parseArray(a,String.class);
		System.out.println(newa.toString());
	}
}

结果:

{"age":21,"name":"刘XX"} 刘XX 21 [{"age":21,"name":"刘XX"},{"age":21,"name":"大XX"}] 刘XX 21 大XX 21 ["aaa","bbb","ccc"] [aaa,bbb,ccc]

原文链接:https://www.f2er.com/json/289993.html

猜你在找的Json相关文章