我们在做项目的时候通常会遇到JSON字符串,我们所需要的数据就在这JSON字符串中,所以我们需要把JSON字符串中的数据拿出来,接下来我将告诉大家如何操作。
{
"country": "中国",
"province": [{
"city": "西安",
"postoffice": 710126,
"school": {
"name": "西安电子科技大学",
"major": "computer"
},
"capital": "true"
},
{
"city": "北京",
"postoffice": 826123,
"school": {
"name": "北京大学",
"major": "fiance"
},
"capital": "true"
},
{
"city": "温州市",
"postoffice": 236556,
"school": {
"name": "浙江大学",
"major": "accounting"
},
"capital": "false"
}
]
}
首先查看我么接收到的JSON字符串在JSON校验器上检验查看JSON是否合法,如果校验器显示为正确的JSON这时候我们开始进行对象转换工作。
image.png
接着我们在我们项目的pom文件中中加入相关的依赖。
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
然后对照JSON的形式编写对应的java类(一般{} 对应的java类,[]对应的数组, :对应java类中的属性)
public class InternationalSchool implements Serializable{
private String country;
private List<Province> province;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public List<Province> getProvince() {
return province;
}
public void setProvince(List<Province> province) {
this.province = province;
}
@Override
public String toString() {
return "InternationalSchool{" +
"country='" + country + '\'' +
", province=" + province +
'}';
}
}
public class Province implements Serializable{ {
private String city;
private String postoffice;
private List<School> school;
private boolean capital;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostoffice() {
return postoffice;
}
public void setPostoffice(String postoffice) {
this.postoffice = postoffice;
}
public List<School> getSchool() {
return school;
}
public void setSchool(List<School> school) {
this.school = school;
}
public boolean isCapital() {
return capital;
}
public void setCapital(boolean capital) {
this.capital = capital;
}
@Override
public String toString() {
return "Province{" +
"city='" + city + '\'' +
", postoffice='" + postoffice + '\'' +
", school=" + school +
", capital=" + capital +
'}';
}
}
public class School implements Serializable{ {
private String name;
private String major;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", major='" + major + '\'' +
'}';
}
}
接着我们编写测试类进行测试。
import net.sf.json.JSONObject;
import java.util.List;
public class Demo {
public static void main(String[] args) {
String jsonData = "{\n" +
"\t\"country\": \"中国\",\n" +
"\t\"province\": [{\n" +
"\t\t\t\"city\": \"西安\",\n" +
"\t\t\t\"postoffice\": 710126,\n" +
"\t\t\t\"school\": {\n" +
"\t\t\t\t\"name\": \"西安电子科技大学\",\n" +
"\t\t\t\t\"major\": \"computer\"\n" +
"\t\t\t},\n" +
"\t\t\t\"capital\": \"true\"\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\"city\": \"北京\",\n" +
"\t\t\t\"postoffice\": 826123,\n" +
"\t\t\t\"school\": {\n" +
"\t\t\t\t\"name\": \"北京大学\",\n" +
"\t\t\t\t\"major\": \"fiance\"\n" +
"\t\t\t},\n" +
"\t\t\t\"capital\": \"true\"\n" +
"\t\t},\n" +
"\t\t{\n" +
"\t\t\t\"city\": \"温州市\",\n" +
"\t\t\t\"postoffice\": 236556,\n" +
"\t\t\t\"school\": {\n" +
"\t\t\t\t\"name\": \"浙江大学\",\n" +
"\t\t\t\t\"major\": \"accounting\"\n" +
"\t\t\t},\n" +
"\t\t\t\"capital\": \"false\"\n" +
"\t\t}\n" +
"\t]\n" +
"}";
JSONObject jsonObject= JSONObject.fromObject(jsonData);
InternationalSchool inSchool = (InternationalSchool) JSONObject.toBean(jsonObject,InternationalSchool.class);
System.out.println(inSchool);
String country = inSchool.getCountry();
List<Province> provinces = inSchool.getProvince();
System.out.println("打印provinces的到的是provinces中的结果");
}
}
最后查看运行结果
image.png
总结:JSON对象和JAVA对象转换常见的情况。
JSON 对象装换为JAVA对象
JSONObject jsonObject= JSONObject.fromObject(jsonData);
InternationalSchool inSchool = (InternationalSchool) JSONObject.toBean(jsonObject,InternationalSchool.class);
JAVA对象转换为JSON对象
School school = new School();
school.setMajor("computer");
school.setName("西安电子科技大学");
JSONObject jsonData = JSONObject.fromObject(school);
System.out.println(jsonData);
JSONArray对象转为JAVA对象
JSONArray array = JSONArray.fromObject("province ");
List<Person> list = JSONArray.toList(array, Person.class);
网友评论