Fastjson是一个Java语言编写的JSON处理器。 1、遵循http://json.org标准,为其官方网站收录的参考实现之一。 2、功能qiang打,支持JDK的各种类型,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。 3、无依赖,不需要例外额外的jar,能够直接跑在JDK上。 4、开源,使用Apache License 2.0协议开源。http://code.alibabatech.com/wiki/display/FastJSON/Home
准备工作:
1.下载或maven 依赖 fastjson jar
废话不说还是上代码,代码是最好的老师,不容置疑!
package com.gscDemo.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class Demo2 {
private static String s;
public static void main(String[] args) {
ToJson();
ParseJSON();
}
private static void ParseJSON() {
User u = new User(1001, "fdy", 34);
String s = JSON.toJSONString(u);
u = JSON.parseObject(s,User.class);
System.out.println("====="+u.toString());
}
private static void ToJson() {
User u = new User(1001, "fdy", 34);
User u1 = new User(1002, "gtf", 23);
User u2 = new User(1003, "djy", 23);
User u3 = new User(1004, "lbs", 24);
s = JSON.toJSONString(u);
System.out.println("===" + s);
List<User> l = new ArrayList<User>();
l.add(u);
l.add(u1);
l.add(u2);
l.add(u3);
s = JSON.toJSONString(l);
System.out.println("===3" + s);
List<String> a = new ArrayList<>();
a.add("100");
a.add("fdy");
a.add("Android");
s = JSON.toJSONString(a);
System.out.println("===4" + s);
Map<String, String> map = new HashMap<>();
map.put("1", "fdy");
map.put("2", "lyc");
map.put("3", "pxc");
List<Map> j = new ArrayList<>();
j.add(map);
s = JSON.toJSONString(j);
System.out.println("map==" + s);
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
public class Demo2 {
private static String s;
public static void main(String[] args) {
ToJson();
ParseJSON();
}
private static void ParseJSON() {
User u = new User(1001, "fdy", 34);
String s = JSON.toJSONString(u);
u = JSON.parseObject(s,User.class);
System.out.println("====="+u.toString());
}
private static void ToJson() {
User u = new User(1001, "fdy", 34);
User u1 = new User(1002, "gtf", 23);
User u2 = new User(1003, "djy", 23);
User u3 = new User(1004, "lbs", 24);
s = JSON.toJSONString(u);
System.out.println("===" + s);
List<User> l = new ArrayList<User>();
l.add(u);
l.add(u1);
l.add(u2);
l.add(u3);
s = JSON.toJSONString(l);
System.out.println("===3" + s);
List<String> a = new ArrayList<>();
a.add("100");
a.add("fdy");
a.add("Android");
s = JSON.toJSONString(a);
System.out.println("===4" + s);
Map<String, String> map = new HashMap<>();
map.put("1", "fdy");
map.put("2", "lyc");
map.put("3", "pxc");
List<Map> j = new ArrayList<>();
j.add(map);
s = JSON.toJSONString(j);
System.out.println("map==" + s);
}
}
User 一个domin
package com.gscDemo.test;
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
super(); //此处注意忽略 会报错: Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class
com.gscDemo.test.User
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
import java.io.Serializable;
public class User implements Serializable {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public User() {
super(); //此处注意忽略 会报错: Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class
com.gscDemo.test.User
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
本文介绍了一个高效的Java JSON处理器Fastjson的使用方法,包括JSON对象的序列化与反序列化过程,并通过实例展示了如何将Java对象转换为JSON字符串及从JSON字符串解析回Java对象。

597

被折叠的 条评论
为什么被折叠?



