使用FastJson转化Json格式
2021-06-09 13:03
标签:color epo ide bsp override entry return lis enc http://repo1.maven.org/maven2/com/alibaba/fastjson/ 使用FastJson转化Json格式 标签:color epo ide bsp override entry return lis enc 原文地址:http://www.cnblogs.com/NEWHOM/p/7299634.html1.下载Jar包
2.将jar包导入工程
3.示例
package nc.testFastJson;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
public class TestFastJson {
public static void main(String[] args) {
// java对象 转 json
People p1 = new People("people_1","Male",1);
String p1_Json = JSON.toJSONString(p1);
System.out.println(p1_Json.toString());
// json 转 java对象
String p2_Json = "{‘name‘:‘people_2‘,‘sex‘:‘Male‘,‘age‘:2}";
People p2 = JSON.parseObject(p2_Json, People.class);
System.out.println(p2.toString());
// java对象LinkedList集合 转 json
LinkedList
package nc.testFastJson;
public class People {
private String name ;
private String sex ;
private int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
public People() {
super();
}
public People(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
}