概念
- String:这个很好解释,指使用“”双引号或’’单引号包括的字符。例如:
String comStr = "this is string; "
- Json:指的是符合json格式要求的字符串。例如:
String jsonStr = "{StudentID:'100',Name:'tmac',Hometown:'usa'}";
- JSONObject:指符合json格式要求的对象,可以看做一个Map<String,Object>。例如:
JSONObject jsonObj = { StudentID: "100", Name: "tmac", Hometown: "usa" };
- JSONArray:当做一个List<Object>,可以把JSONArray看成JSONObject对象的一个集合
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}]}";
json字符串---->JSONObject
/**
* json字符串-简单对象型与JSONObject之间的转换
*/
@Test
public 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"));
String jsonStr = JSONObject.toJSONString(jsonObject);
System.out.println(jsonStr);
}
json字符串---->JSONArray
/**
* json字符串-数组类型与JSONArray之间的转换
*/
@Test
public 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"));
}
//遍历方式2
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
json字符串---->JSONObject【非重点】
/**
* 复杂json格式字符串与JSONObject之间的转换
*/
@Test
public void testComplexJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
//JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
String teacherName = jsonObject.getString("teacherName");
Integer teacherAge = jsonObject.getInteger("teacherAge");
JSONObject course = jsonObject.getJSONObject("course");
JSONArray students = jsonObject.getJSONArray("students");
Student s = new Student();
s.setStudentName("王五");
s.setStudentAge(12);
JSONObject obj = (JSONObject) JSON.toJSON(s);
//System.out.println(obj.toJSONString());
Map smap = JSONObject.parseObject(obj.toJSONString(), Map.class);
//System.out.println(smap);
String str=JSONObject.toJSONString((Object)s);
Map smap2 = JSONObject.parseObject(str, Map.class);
//System.out.println(smap2);
Map<String,Object> map = new HashMap<String,Object>();
map.put("studentName", "张三");
map.put("studentAge", 20);
String strmap=JSONObject.toJSONString((Object)map);
Student stu = JSONObject.parseObject(strmap, Student.class);
System.out.println(stu);
}
json字符串---->Bean 【重点】
/**
* json字符串-简单对象与JavaBean_obj之间的转换[重点]
*/
@Test
public void testJSONStrToJavaBeanObj(){
Student student = JSON.parseObject(JSON_OBJ_STR, Student.class);
//Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});//因为JSONObject继承了JSON,所以这样也是可以的
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
json字符串---->List<Bean>【重点】
/**
* json字符串-数组类型与JavaBean_List之间的转换
*/
@Test
public void testJSONStrToJavaBeanList(){
ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
//ArrayList<Student> students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});//因为JSONArray继承了JSON,所以这样也是可以的
for (Student student : students) {
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
}