如何将JSON字符串解析为java对象
我有一个JSON字符串,我想从中提取值如何将JSON字符串解析为java对象,java,json,Java,Json,我有一个JSON字符串,我想从中提取值 @RequestMapping(value = "/userrecords",method = RequestMethod.POST) public @ResponseBody Response saveList(@RequestBody String str,Model model) throws ParseException, SQLIntegrityConstraintViolationException { System.out.println("
@RequestMapping(value = "/userrecords",method = RequestMethod.POST)
public @ResponseBody Response saveList(@RequestBody String str,Model model)
throws ParseException, SQLIntegrityConstraintViolationException {
System.out.println("--welcome--"+str.toString());//output:[{"name":"xxx","email":"xxx@gmail.com","message":"sdfasd"},{"name":"yyy","email":"yyy@gmail.com","message":"sdfasd"}]
//here i want all names,emails and messages
}
我有一个带有setter和getter的pojo类User
。您可以使用
Gson gson = new Gson();
User user = gson.fromJson(str.toString(), User.class);
给那个画一个复制品。那个是给Java ME的,不是普通的Java。@Japu\u-D\u-Cret:对不起,我这里有多个JSON。@Japu\u-D\u-Cret:这不是重复的。请看我的输出。在你确定它是否重复后。很抱歉,它是重复的。将JSON转换为POJO后,通过调用getter“提取”值。这与解析无关,而且。。。坦白地说琐碎的。输出值的“问题”也是如此。如果不是的话,你在表达你在这里实际要求的东西方面做得很差。@Stephen:我找到了解决办法。通过使用JSONArray(字符串)