本文共 1572 字,大约阅读时间需要 5 分钟。
User user = new User(1L, "haha", 13, "erfa",new Date(),new Date(),1,3);@Testvoid testFastJson() { //将对象转换成json字符串 String userToJsonString = JSON.toJSONString(user); System.out.println(userToJsonString); //将json字符串转换成对象 User jsonStringToUser = JSON.parseObject(userToJsonString,User.class); System.out.println(jsonStringToUser);}/** * JavaBean转JSONObject */@Testvoid JavaBeanToJSONObject(){ JSONObject jsonObject = (JSONObject)JSONObject.toJSON(user); System.out.println(jsonObject.toString());}/** * JavaBean转JSON */@Testvoid JavaBeanToJSON(){ String json = JSON.toJSONString(user); System.out.println(json);}/** * JSONObject转JavaBean */@Testvoid JSONObjectToJavaBean() { JSONObject jsonObject = (JSONObject)JSONObject.toJSON(user); User user = jsonObject.toJavaObject(User.class); System.out.println(user.toString());}/** * JSONObject转Json */@Testvoid JSONObjectToJson() { JSONObject jsonObject = (JSONObject)JSONObject.toJSON(user); String json = JSONObject.toJSONString(jsonObject); System.out.println(json);}/** *Json转JavaBean */@Testvoid JsonToJavaBean() { String json = JSON.toJSONString(user); User user = JSON.parseObject(json, User.class); System.out.println(user.toString());}/** * Json转JSONObject */@Testvoid JsonToJSONObject() { String json = JSON.toJSONString(user); JSONObject jsonObject = JSONObject.parseObject(json); System.out.println(jsonObject); String name = jsonObject.getString("name"); System.out.println(name);}
转载地址:http://owgvz.baihongyu.com/