JSON类型转换
fastjson json转某个对象
String requestStr = "{\"s_user_account\":\"15573232053\",\"s_user_name\":\"胡先生的店\",\"ip\":\"10.18.132.163\",\"Referer\":\"http://172.20.6.24:8083/p/machines/down.html\",\"pageSize\":\"500\",\"s_user_id\":\"50267000\",\"UA\":\"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36\",\"token\":\"e575fc9c13ec4835bb0dcf7b32b4a640\",\"command_id\":\"ListPhysnRpc\",\"domin\":\"172.20.6.24:8083\",\"pageIndex\":\"1\",\"service_id\":\"ListPhysnRpc\",\"startPhysn\":\"JLSC141769\",\"logId\":\"260d49ed262844f685d97d30c0244e40\",\"operatorId\":\"50267000\",\"_sourceIp\":\"10.18.132.163\",\"bindStatus\":\"1\",\"_sourcePlatType\":\"WEB\"}\n"; PhysnListRequest request = JSON.parseObject(requestStr, PhysnListRequest.class);
String objectJSONStr = "{\n" + " \"id\":\"15573232053\",\n" + " \"name\":\"杰森\",\n" + "}"; Student stu = JSON.parseObject(objectJSONStr, Student.class); System.out.println("JSON to Object: " + stu.toString()); String jsonStr = JSON.toJSONString(stu); System.out.println("object to jsonString: " + jsonStr); String jsonList = "[{\n" + " \"id\":\"001\",\n" + " \"name\":\"杰森\",\n" + "},{\n" + " \"id\":\"001\",\n" + " \"name\":\"杰森\",\n" + "}]"; List<Student> studentList = JSON.parseArray(jsonList, Student.class); System.out.println("json to List: " + studentList.toString()); String strList = JSON.toJSONString(studentList); System.out.println("list to json: " + strList.toString());
与javascript的转换
JSON.parse():用于将一个 JSON 字符串转换为 JavaScript 对象。
JSON.stringify():用于将 JavaScript 值转换为 JSON 字符串。