将Java对象转换为JSONObject并在GET方法中传输它。 我正在开发一个Android应用程序,为此我还开发了一个 基于springmvc的服务器。不幸的是,在此之前,我没有这样做 那么多关于JSONObject的工作。目前,我能够发送Java 对象从Android应用程序发送到服务器,并接收Java对象 也 我对使用谷歌提供的截击框架感兴趣, 这将避免Asynctask的麻烦,而且效率更高,但是 它处理JSONObject 不幸的是,无论我在网上看到什么地方,我都发现了 创建JSONObject以将其保存在本地硬盘上的某个文件中,但 不,我想把它们转寄到回信本上,有人能帮我吗 创建一个JAVA对象到JSOBObject,反之亦然。我有 在servlet上下文中设置的所有POM依赖项和MessageConverter
控制器代码当前:将Java对象转换为JSONObject并在GET方法中传输它。 我正在开发一个Android应用程序,为此我还开发了一个 基于springmvc的服务器。不幸的是,在此之前,我没有这样做 那么多关于JSONObject的工作。目前,我能够发送Java 对象从Android应用程序发送到服务器,并接收Java对象 也 我对使用谷歌提供的截击框架感兴趣, 这将避免Asynctask的麻烦,而且效率更高,但是 它处理JSONObject 不幸的是,无论我在网上看到什么地方,我都发现了 创建JSONObject以将其保存在本地硬盘上的某个文件中,但 不,我想把它们转寄到回信本上,有人能帮我吗 创建一个JAVA对象到JSOBObject,反之亦然。我有 在servlet上下文中设置的所有POM依赖项和MessageConverter,json,spring,spring-mvc,Json,Spring,Spring Mvc,控制器代码当前: //Restaurant is just a plain Java class, I can give it as a JSONObject, but I dont know how to convert that JSONObject to java so I can save the restaurant in the server. @RequestMapping(value = "/restaurant/add",method = RequestMethod.POST
//Restaurant is just a plain Java class, I can give it as a JSONObject, but I dont know how to convert that JSONObject to java so I can save the restaurant in the server.
@RequestMapping(value = "/restaurant/add",method = RequestMethod.POST)
@ResponseBody
public String addRestaurantWebView(@RequestBody Restaurant restaurant){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("restaurant", new Restaurant());
modelAndView.addObject(restaurant);
this.restaurantService.addRestaurant(restaurant);
return "true";
}
//Similarly, here, I don't know how to convert the Restaurant's list to JSONObject when there is a get Request.
@RequestMapping(value = "/restaurant/listing", method = RequestMethod.GET)
public @ResponseBody List<Restaurant> listAllRestaurants(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("restaurant", new Restaurant());
List<Restaurant> restaurantList = this.restaurantService.listRestaurants();
modelAndView.addObject("listRestaurant", restaurantList);
return restaurantList;
}
//Restaurant只是一个普通的Java类,我可以将其作为JSONObject提供,但我不知道如何将该JSONObject转换为Java,以便将Restaurant保存在服务器中。
@RequestMapping(value=“/restaurant/add”,method=RequestMethod.POST)
@应答器
公共字符串addRestaurantWebView(@RequestBody Restaurant Restaurant){
ModelAndView ModelAndView=新建ModelAndView();
addObject(“餐厅”,新餐厅());
modelAndView.addObject(餐厅);
这个.restaurantService.addRestaurant(餐厅);
返回“真”;
}
//类似地,在这里,当有get请求时,我不知道如何将餐厅列表转换为JSONObject。
@RequestMapping(value=“/restaurant/listing”,method=RequestMethod.GET)
public@responseboody List listAllRestaurants(){
ModelAndView ModelAndView=新建ModelAndView();
addObject(“餐厅”,新餐厅());
List restaurantList=this.restaurantService.listRestaurants();
添加对象(“listRestaurant”,restaurantList);
返回餐厅列表;
}
我希望我的问题是明确的,如果有任何疑问,请让我知道。非常感谢 看一看。这是一个非常简洁的API,用于将对象转换为JSON。通过将类中的@Expose注释添加到需要包含的属性中,可以轻松地指定属性。试着这样做:
@RequestMapping(value = "/restaurant/listing", method = RequestMethod.GET)
public @ResponseBody String listAllRestaurants(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("restaurant", new Restaurant());
List<Restaurant> restaurantList = this.restaurantService.listRestaurants();
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
String jsonString = gson.toJson(restaurantList);
return jsonString;
}
@RequestMapping(value=“/restaurant/listing”,method=RequestMethod.GET)
public@ResponseBody字符串listAllRestaurants(){
ModelAndView ModelAndView=新建ModelAndView();
addObject(“餐厅”,新餐厅());
List restaurantList=this.restaurantService.listRestaurants();
Gson Gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
字符串jsonString=gson.toJson(restaurantList);
返回jsonString;
}
不必用@Expose注释属性,但如果最终有任何循环引用,则会有所帮助
祝你好运