1、添加依赖
<!--添加fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
2、
package com.test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class JsonTest {
//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}]}";
public static void main(String[] args) {
System.out.println("kaishi");
System.out.println(JSON_OBJ_STR);
System.out.println(JSON_ARRAY_STR );
System.out.println(COMPLEX_JSON_STR);
testJSONStrToJSONObject();
System.out.println("-----------------");
testJSONStrToJSONArray();
System.out.println("方式三-----------------");
testComplexJSONStrToJSONObject();
}
/**
* json字符串-简单对象型与JSONObject之间的转换
*/
public static void testJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
/**
* json字符串-数组类型与JSONArray之间的转换
*/
public static void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
//JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的
//遍历方式1
int size = jsonArray.size();
for (int i = 0; i < size; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
System.out.println("遍历方式二");
//遍历方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
/**
* 复杂json格式字符串与JSONObject之间的转换
*/
public static void testComplexJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
String teacherName = jsonObject.getString("teacherName");
System.out.println(teacherName);
Integer teacherAge = jsonObject.getInteger("teacherAge");
System.out.println(teacherAge);
JSONObject course = jsonObject.getJSONObject("course");
System.out.println(course);
JSONArray students = jsonObject.getJSONArray("students");
System.out.println(students);
//遍历方式1
int size = students.size();
for (int i = 0; i < size; i++){
JSONObject jsonObjectstudent = students.getJSONObject(i);
System.out.println(jsonObjectstudent .getString("studentName")+":"+jsonObjectstudent .getInteger("studentAge"));
}
}
}