概念
- 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,可以把JSONArray看成JSONObject对象的一个集合
Json字符串
private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";
private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";
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
@Test
public void testJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
String jsonStr = JSONObject.toJSONString(jsonObject);
System.out.println(jsonStr);
}
json字符串---->JSONArray
@Test
public void testJSONStrToJSONArray(){
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
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"));
}
for (Object obj : jsonArray) {
JSONObject jsonObject = (JSONObject) obj;
System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
}
}
json字符串---->JSONObject【非重点】
@Test
public void testComplexJSONStrToJSONObject(){
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
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);
Map smap = JSONObject.parseObject(obj.toJSONString(), Map.class);
String str=JSONObject.toJSONString((Object)s);
Map smap2 = JSONObject.parseObject(str, Map.class);
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 【重点】
@Test
public void testJSONStrToJavaBeanObj(){
Student student = JSON.parseObject(JSON_OBJ_STR, Student.class);
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
json字符串---->List【重点】
@Test
public void testJSONStrToJavaBeanList(){
ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
for (Student student : students) {
System.out.println(student.getStudentName()+":"+student.getStudentAge());
}
}