使用java解析及转换JSON
1.通过java解析
2.使用阿里巴巴提供的json解析或转换
3.使用谷歌提供的Gson解析或转换
json:
解析JSON对象 JSON.parseObject(JSON_OBJ_STR, Student.class);
解析JSON集合 JSON.parseArray(JSON_OBJ_STR3)
转换为JSON字符串 JSON.toJSONString
Gson:
解析需new出新对象 Gson gson = new Gson();
解析JSON对象、集合 gson.fromJson(JSON_OBJ_STR6,new TypeToken<List<Student>>(){}.getType());
转换为JSON字符串 gson.toJson(JSON_OBJ_STR9);
配置:
1.使用了myeclipse进行解析或转换2.创建一个新的项目Web Project
3.要在WEB-INF下的lib添加压缩文件,直接复制进去即可
4.要添加依赖,直接点击右键有个build Path 选择add点击即可,添加成功后会在下图文件夹中多出两个文件
思路:
1.根据解析的内容需要先创建一个对象类,让其实现Serializable接口,并新建属性和setget方法
2.新建测试类中JSON或gson点出相应的方法进行输出或则遍历
对象类参考代码:
public class Student implements Serializable{
private String studentName;
private int studentAge;
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}
}
测试类参考代码:
public class Text {
public static void main(String[] args) {
/*json解析对象*/
/*通过JSON.parseObject(JSON_OBJ_STR, Student.class);解析*/
String JSON_OBJ_STR="{\"studentName\":\"呵呵\",\"studentAge\":\"20\"}";
Student student = JSON.parseObject(JSON_OBJ_STR, Student.class);
System.out.println(student.getStudentName()+"\t"+student.getStudentAge());
/*通过获取键值解析*/
String JSON_OBJ_STR2="{\"studentName\":\"哈哈\",\"studentAge\":\"18\"}";
JSONObject student1 = JSON.parseObject(JSON_OBJ_STR2);
System.out.println(student1.getString("studentName")+"\t"+student1.getString("studentAge"));
/*集合通过获取键值解析*/
String JSON_OBJ_STR3= "[{\"studentName\":\"吴师傅\",\"studentAge\":\"20\"},{\"studentName\":\"绅士冼\",\"studentAge\":\"23\"}]";
List<Object>list1 = JSON.parseArray(JSON_OBJ_STR3);
for (Object l:list1) {
System.out.println(((JSONObject)l).getString("studentName")+"\t"+((JSONObject)l).getInteger("studentAge"));
}
/*json解析集合对象*/
String JSON_OBJ_STR4= "[{\"studentName\":\"吴师傅\",\"studentAge\":\"20\"},{\"studentName\":\"绅士冼\",\"studentAge\":\"23\"}]";
List<Student> list2 = JSON.parseArray(JSON_OBJ_STR4,Student.class);
for (Student s:list2) {
System.out.println(s.getStudentName()+"\t"+s.getStudentAge());
}
/*把集合转换成JSON字符串*/
String JSON_OBJ_STR5= "[{\"studentName\":\"吴师傅\",\"studentAge\":\"20\"},{\"studentName\":\"绅士冼\",\"studentAge\":\"23\"}]";
List<Student>list3 = JSON.parseArray(JSON_OBJ_STR3,Student.class);
String str = JSON.toJSONString(list3);
System.out.println(str);
/*使用Gson解析集合*/
String JSON_OBJ_STR6= "[{\"studentName\":\"吴师傅\",\"studentAge\":\"20\"},{\"studentName\":\"绅士冼\",\"studentAge\":\"23\"}]";
Gson gson = new Gson();
List<Student> students = gson.fromJson(JSON_OBJ_STR6,new TypeToken<List<Student>>(){}.getType());
for (int i = 0; i <students.size(); i++) {
System.out.println(students.get(i).getStudentName()+"\t"+students.get(i).getStudentAge());
}
/*使用Gson解析对象*/
/*方法一*/
String JSON_OBJ_STR7="{\"studentName\":\"哈哈\",\"studentAge\":\"18\"}";
Student students2 = gson.fromJson(JSON_OBJ_STR7,new TypeToken<Student>(){}.getType());
System.out.println(students2.getStudentName()+"\t"+students2.getStudentAge());
/*方法二*/
String JSON_OBJ_STR8="{\"studentName\":\"呵呵\",\"studentAge\":\"20\"}";
Student students3 = gson.fromJson(JSON_OBJ_STR8,Student.class);
System.out.println(students3.getStudentName()+"\t"+students3.getStudentAge());
/*把对象转换Gson*/
String JSON_OBJ_STR9="{\"studentName\":\"呵呵\",\"studentAge\":\"20\"}";
String str1 = gson.toJson(JSON_OBJ_STR9);
System.out.println(str1);
/*把集合转换Gson*/
String JSON_OBJ_STR10="[{\"studentName\":\"吴师傅\",\"studentAge\":\"20\"},{\"studentName\":\"绅士冼\",\"studentAge\":\"23\"}]";
String str2 = gson.toJson(JSON_OBJ_STR10);
System.out.println(str2);
}
}