/**
* json字符串转成json对象
*/
@Test
public void test036(){
/*原始json字符串*/
System.out.println("--------------------------------------------");
System.out.println("原始json字符串");
String json_obj_str = "{\"userName\":\"zhanqian\",\"userAge\":26}";
System.out.println(json_obj_str);
/*1、json字符串转成json对象*/
JSONObject json_obj = JSON.parseObject(json_obj_str);
System.out.println("--------------------------------------------");
System.out.println("json字符串转成json对象");
System.out.println(json_obj);
System.out.println("--------------------------------------------");
System.out.println("获取对象属性");
System.out.println(json_obj.getString("userName")+":"+json_obj.getInteger("userAge"));
/*2、将json对象转成json字符串*/
System.out.println("--------------------------------------------");
System.out.println("将json对象转成json字符串");
String json_str = JSON.toJSONString(json_obj);
System.out.println(json_str);
/*3、将json字符串反序列化成javaBean对象*/
System.out.println("--------------------------------------------");
System.out.println("将json字符串反序列化成javaBean对象");
String text = "{\"money\":105,\"username\":\"zhaoqian\"}";
User user = (User) JSON.parseObject(text, User.class);
System.out.println("JavaBean的toString():" + user.toString());
/*4、将javaBean转化为json对象 */
System.out.println("--------------------------------------------");
System.out.println("将javaBean转化为json对象 ");
User user2 = new User("sunli", 299);
JSONObject jsonObj = (JSONObject) JSON.toJSON(user2);
System.out.println(jsonObj);
/*5、全序列化 直接把java bean序列化为json文本之后,按照原来的类型反序列化回来*/
User user3 = new User("zhouwu", 999);
SerializerFeature[] featureArr = { SerializerFeature.WriteClassName };
String text3 = JSON.toJSONString(user3, featureArr);
System.out.println("--------------------------------------------");
System.out.println("全序列化 ");
System.out.println(text3);
User userObj3 = (User) JSON.parse(text3);
System.out.println("--------------------------------------------");
System.out.println("序列化回来 ");
System.out.println(userObj3.toString());
//json字符串-简单对象型
String JSON_OBJ_STR = "{\"name\":\"lily\",\"score\":12}";
//json字符串-数组类型
String JSON_ARRAY_STR = "[{\"name\":\"lily\",\"score\":12},{\"name\":\"lucy\",\"score\":15}]";
//复杂格式json字符串
String COMPLEX_JSON_STR = "{\"teacherName\":\"crystall\",\"teacherAge\":27," +
"\"course\":{\"courseName\":\"english\",\"code\":1270}," +
"\"students\":[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]}";
System.out.println("1json字符串-数组类型--------------------------------------------");
System.out.println(JSON_ARRAY_STR);
System.out.println("2复杂格式json字符串--------------------------------------------");
System.out.println(COMPLEX_JSON_STR);
System.out.println("3json字符串-数组类型转成JSONArray--------------------------------------------");
JSONArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
System.out.println(jsonArray);
System.out.println("4.1json数组字符串与JavaBean_List之间的转换--------------------------------------------");
ArrayList<Student> students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList<Student>>() {});
for(Student s:students){
System.out.println(s.toString());
}
System.out.println("4.2json数组字符串与JavaBean_obj之间的转换--------------------------------------------");
Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference<Student>() {});
System.out.println(student.toString());
System.out.println("复杂json格式字符串与JSONObject之间的转换--------------------------------------------");
JSONObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
String teacherName = jsonObject.getString("teacherName");
System.out.println(teacherName);
System.out.println("复杂json格式字符串与JavaBean_obj之间的转换--------------------------------------------");
Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference<Teacher>() {});
System.out.println(teacher.toString());
/*字符串替换,替换掉前后的方括号*/
if(JSON_ARRAY_STR.indexOf("[") != -1){
JSON_ARRAY_STR = JSON_ARRAY_STR.replace("[", "");
}
System.out.println(JSON_ARRAY_STR);
if(JSON_ARRAY_STR.indexOf("]") != -1){
JSON_ARRAY_STR = JSON_ARRAY_STR.replace("]", "");
}
System.out.println(JSON_ARRAY_STR);
}