gson转换
2019-11-28 本文已影响0人
就叫basi
case 1
customerListJson:"[{"customerName":"王","customerShortName":"","businessUserName":"郭经理","customerPersonName":"王的2","phone":"534595"},
{"customerName":"吴","customerShortName":"","businessUserName":"郭经理","customerPersonName":"吴的1","phone":"34962734"},
{"customerName":"赵","customerShortName":"","businessUserName":"张销售","customerPersonName":null,"phone":null}]"
@RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody
public void customerList(@RequestBody JSONObject info){
List<Customer> custcomerList =
JSONObject.parseArray(info.getJSONArray("customerListJson").toJSONString(), Customer.class);
}
case 2
list接 @RequestMapping(value = "/", method = RequestMethod.POST)
@ResponseBody
public void customerList(@RequestBody Map<String, Object> map)
{
List<OutsideMessage> messageList =
JSONArray.parseArray(JSON.toJSONString(map.get("message")), OutsideMessage.class);
}
//数据准备
List<Student> list=new ArrayList<Student>();
list.add(new Student(1,1000d,"旅游",new Date()));
list.add(new Student(2,1500d,"玩电脑游戏",new Date()));
list.add(new Student(3,3000d,"看电影",new Date()));
Student student=new Student(1,1000d,"旅游",new Date());
//实例化Gson对象
Gson gson=new Gson();
//将对象转为JSON字符串
String json = gson.toJson(student); //{"index":1,"size":1000.0,"string":"旅游","date":"Nov 28, 2019 11:46:51 AM"}
//将JSON字符串转为对象
Student stu = gson.fromJson(json, Student.class);
//将List ---->JSON字符串
String json2 = gson.toJson(list);
//:[{"index":1,"size":1000.0,"string":"旅游","date":"Nov 28, 2019 11:46:51 AM"},
// {"index":2,"size":1500.0,"string":"玩电脑游戏","date":"Nov 28, 2019 11:46:51 AM"},
// {"index":3,"size":3000.0,"string":"看电影","date":"Nov 28, 2019 11:46:51 AM"}]
//将JSON字符串---->List
List<Student> stuList = gson.fromJson(json2, new TypeToken<List<Student>>(){}.getType());
vans.png