fastJson对于json格式字符串的解析主要用到了一下三个类:
JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。
JSONObject:fastJson提供的json对象。
JSONArray:fastJson提供json数组对象。
我们可以把JSONObject当成一个Map<String,Object>来看,只是JSONObject提供了更为丰富便捷的方法,方便我们对于对象属性的操作。我们看一下源码。
![](https://i-blog.csdnimg.cn/blog_migrate/1dacebcc89904ab9597a16c2b75e751a.png)
同样我们可以把JSONArray当做一个List<Object>,可以把JSONArray看成JSONObject对象的一个集合。
![](https://i-blog.csdnimg.cn/blog_migrate/59441f88dfc143bb59bbbc00667308ab.png)
此外,由于JSONObject和JSONArray继承了JSON,所以说也可以直接使用两者对JSON格式字符串与JSON对象及javaBean之间做转换,不过为了避免混淆我们还是使用JSON。
首先定义三个json格式的字符串,作为我们的数据源。
//json字符串-简单对象型
private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
//json字符串-数组类型
private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
//复杂格式json字符串
private static final String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27,\"course\":{\"courseName\":\"english\",\"code\":1270},\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
示例1:JSON格式字符串与JSON对象之间的转换。
示例1.1-json字符串-简单对象型与JSONObject之间的转换
/**
* json字符串-简单对象型与JSONObject之间的转换
*/
public static void testJSONStrToJSONObject(){
JSONObject jsonObject </span>=<span style="color: #000000"> JSON.parseObject(JSON_OBJ_STR);
</span><span style="color: #008000">//</span><span style="color: #008000">JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); </span><span style="color: #008000">//</span><span style="color: #008000">因为JSONObject继承了JSON,所以这样也是可以的</span>
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"
));
}</span></pre>
示例1.2-json字符串-数组类型与JSONArray之间的转换
/**
* json字符串-数组类型与JSONArray之间的转换
*/
public static void testJSONStrToJSONArray(){
JSONArray jsonArray </span>=<span style="color: #000000"> JSON.parseArray(JSON_ARRAY_STR);
</span><span style="color: #008000">//</span><span style="color: #008000">JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);</span><span style="color: #008000">//</span><span style="color: #008000">因为JSONArray继承了JSON,所以这样也是可以的
</span><span style="color: #008000">//</span><span style="color: #008000">遍历方式1</span>
<span style="color: #0000ff">int</span> size =<span style="color: #000000"> jsonArray.size();
</span><span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i < size; i++<span style="color: #000000">){
JSONObject jsonObject </span>=<span style="color: #000000"> jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString(</span>"studentName")+":"+jsonObject.getInteger("studentAge"<span style="color: #000000">));
}
</span><span style="color: #008000">//</span><span style="color: #008000">遍历方式2</span>
<span style="color: #0000ff">for</span><span style="color: #000000"> (Object obj : jsonArray) {
JSONObject jsonObject </span>=<span style="color: #000000"> (JSONObject) obj;
System.out.println(jsonObject.getString(</span>"studentName")+":"+jsonObject.getInteger("studentAge"<span style="color: #000000">));
}
}</span></pre>
示例1.3-复杂json格式字符串与JSONObject之间的转换
/**
* 复杂json格式字符串与JSONObject之间的转换
*/
public static void testComplexJSONStrToJSONObject(){
JSONObject jsonObject </span>=<span style="color: #000000"> JSON.parseObject(COMPLEX_JSON_STR);
</span><span style="color: #008000">//</span><span style="color: #008000">JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);</span><span style="color: #008000">//</span><span style="color: #008000">因为JSONObject继承了JSON,所以这样也是可以的</span>
String teacherName = jsonObject.getString("teacherName"
); Integer teacherAge = jsonObject.getInteger("teacherAge"
); JSONObject course = jsonObject.getJSONObject("course"
); JSONArray students = jsonObject.getJSONArray("students"
);
}</span></pre>
示例2:JSON格式字符串与javaBean之间的转换。
首先,我们针对数据源所示的字符串,提供三个javaBean。
public class Student {
</span><span style="color: #0000ff">private</span><span style="color: #000000"> String studentName;
</span><span style="color: #0000ff">private</span><span style="color: #000000"> Integer studentAge;
</span><span style="color: #0000ff">public</span><span style="color: #000000"> String getStudentName() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> studentName;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setStudentName(String studentName) {
</span><span style="color: #0000ff">this</span>.studentName =<span style="color: #000000"> studentName;
}
</span><span style="color: #0000ff">public</span><span style="color: #000000"> Integer getStudentAge() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> studentAge;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setStudentAge(Integer studentAge) {
</span><span style="color: #0000ff">this</span>.studentAge =<span style="color: #000000"> studentAge;
}
}
public class Course {
</span><span style="color: #0000ff">private</span><span style="color: #000000"> String courseName;
</span><span style="color: #0000ff">private</span><span style="color: #000000"> Integer code;
</span><span style="color: #0000ff">public</span><span style="color: #000000"> String getCourseName() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> courseName;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setCourseName(String courseName) {
</span><span style="color: #0000ff">this</span>.courseName =<span style="color: #000000"> courseName;
}
</span><span style="color: #0000ff">public</span><span style="color: #000000"> Integer getCode() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> code;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setCode(Integer code) {
</span><span style="color: #0000ff">this</span>.code =<span style="color: #000000"> code;
}
}
public class Teacher {
</span><span style="color: #0000ff">private</span><span style="color: #000000"> String teacherName;
</span><span style="color: #0000ff">private</span><span style="color: #000000"> Integer teacherAge;
</span><span style="color: #0000ff">private</span><span style="color: #000000"> Course course;
</span><span style="color: #0000ff">private</span> List<Student><span style="color: #000000"> students;
</span><span style="color: #0000ff">public</span><span style="color: #000000"> String getTeacherName() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> teacherName;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setTeacherName(String teacherName) {
</span><span style="color: #0000ff">this</span>.teacherName =<span style="color: #000000"> teacherName;
}
</span><span style="color: #0000ff">public</span><span style="color: #000000"> Integer getTeacherAge() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> teacherAge;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setTeacherAge(Integer teacherAge) {
</span><span style="color: #0000ff">this</span>.teacherAge =<span style="color: #000000"> teacherAge;
}
</span><span style="color: #0000ff">public</span><span style="color: #000000"> Course getCourse() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> course;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span><span style="color: #000000"> setCourse(Course course) {
</span><span style="color: #0000ff">this</span>.course =<span style="color: #000000"> course;
}
</span><span style="color: #0000ff">public</span> List<Student><span style="color: #000000"> getStudents() {
</span><span style="color: #0000ff">return</span><span style="color: #000000"> students;
}
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setStudents(List<Student><span style="color: #000000"> students) {
</span><span style="color: #0000ff">this</span>.students =<span style="color: #000000"> students;
}
}
json字符串与javaBean之间的转换推荐使用 TypeReference<T> 这个类,使用泛型可以更加清晰,当然也有其它的转换方式,这里就不做探讨了。
示例2.1-json字符串-简单对象型与javaBean之间的转换
/**
* json字符串-简单对象与JavaBean_obj之间的转换
*/
public static void testJSONStrToJavaBeanObj(){
Student student </span>= JSON.parseObject(JSON_OBJ_STR, <span style="color: #0000ff">new</span> TypeReference<Student><span style="color: #000000">() {});
</span><span style="color: #008000">//</span><span style="color: #008000">Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});</span><span style="color: #008000">//</span><span style="color: #008000">因为JSONObject继承了JSON,所以这样也是可以的</span>
System.out.println(student.getStudentName()+":"+
student.getStudentAge());
}</span></pre>
示例2.2-json字符串-数组类型与javaBean之间的转换
/**
* json字符串-数组类型与JavaBean_List之间的转换
*/
public static void testJSONStrToJavaBeanList(){
ArrayList</span><Student> students = JSON.parseObject(JSON_ARRAY_STR, <span style="color: #0000ff">new</span> TypeReference<ArrayList<Student>><span style="color: #000000">() {});
</span><span style="color: #008000">//</span><span style="color: #008000">ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});</span><span style="color: #008000">//</span><span style="color: #008000">因为JSONArray继承了JSON,所以这样也是可以的</span>
<span style="color: #0000ff">for</span><span style="color: #000000"> (Student student : students) {
System.out.println(student.getStudentName()</span>+":"+<span style="color: #000000">student.getStudentAge());
}
}</span></pre>
示例2.3-复杂json格式字符串与与javaBean之间的转换
/**
* 复杂json格式字符串与JavaBean_obj之间的转换
*/
public static void testComplexJSONStrToJavaBean(){
Teacher teacher </span>= JSON.parseObject(COMPLEX_JSON_STR, <span style="color: #0000ff">new</span> TypeReference<Teacher><span style="color: #000000">() {});
</span><span style="color: #008000">//</span><span style="color: #008000">Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});</span><span style="color: #008000">//</span><span style="color: #008000">因为JSONObject继承了JSON,所以这样也是可以的</span>
String teacherName =<span style="color: #000000"> teacher.getTeacherName();
Integer teacherAge </span>=<span style="color: #000000"> teacher.getTeacherAge();
Course course </span>=<span style="color: #000000"> teacher.getCourse();
List</span><Student> students =<span style="color: #000000"> teacher.getStudents();
}</span></pre>
对于TypeReference<T>,由于其构造方法使用 protected 进行修饰,所以在其他包下创建其对象的时候,要用其实现类的子类:new TypeReference<Teacher>() {}
![](https://i-blog.csdnimg.cn/blog_migrate/d86dabef7e1a21949f292e9c1dde0fea.png)
此外的:
1,对于JSON对象与JSON格式字符串的转换可以直接用 toJSONString()这个方法。
2,javaBean与JSON格式字符串之间的转换要用到:JSON.toJSONString(obj);
3,javaBean与json对象间的转换使用:JSON.toJSON(obj),然后使用强制类型转换,JSONObject或者JSONArray。
最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。