一、在maven工程中引入fastjson.jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.41</version>
</dependency>
二、创建实体对象
package json;
public class User {
public String name;
public String age;
public String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
三、创建测试类
package json;
import com.alibaba.fastjson.JSON;
public class JsonTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="{\"name\":\"孙悟空\",\"age\":\"500\",\"sex\":\"火眼晶晶的男猴}";
User user=JSON.parseObject(str, User.class);
System.out.println(user.getName());
}
}