记性差,就记录下来。
1. 字符串转JSONObject:
String extParamStr = orderMqVO.getExtraParam();
JSONObject jsonObject = JSONObject.fromObject(extParamStr);
2. JSONObject 转实体类:
ExtraParamVO extParamVo = (ExtraParamVO) JSONObject.toBean(jsonObject, ExtraParamVO.class);
3. 将java实体类转JSONObject
Student stu1 = new Student("lwc", "111111");
JSONObject jsonObject = JSONObject.fromObject(stu1);
System.out.println(jsonObject);
4. list转数组:
List list = new ArrayList();
list.add(new Student("lwc", "111111"));
list.add(new Student("nxj", "222222"));
JSONArray jsonArray = JSONArray.fromObject(list);
System.out.println(jsonArray);
5. json字符串转list
String jsondata = "[{\"password\":\"111111\",\"username\":\"lwc\"},{\"password\":\"222222\",\"username\":\"nxj\"}]";
JSONArray jsonArray1 = JSONArray.fromObject(jsondata);
for (int i = 0; i < jsonArray1.size(); i++) {
JSONObject jsonObject2 = jsonArray1.getJSONObject(i);
Student stu2 = (Student) JSONObject.toBean(jsonObject2,
Student.class);
list1.add(stu2);
}
System.out.println(list1);