将 Java 对象转换为 JSONObject 并在 GET 方法中传输。
Converting Java object to JSONObject and transmit it at GET method.
- 我正在开发一个 Android 应用程序,为此我也在开发一个
Spring-基于 MVC 的服务器。不幸的是,在此之前,我还没有做过
在 JSONObjects 上做了那么多工作。目前,我可以发送 Java
从 Android 应用程序向服务器发送对象,并接收 Java 个对象
也。
- 我有兴趣使用 Google 提供的 Volley 框架,
这将避免 Asynctask 的麻烦并且效率更高,但是
它处理 JSONObject。
- 不幸的是,无论我在网上看什么,我都找到了代码
创建 JSOnObjects 以将其保存在本地硬盘驱动器上的某个文件中,但是
不,我想在 ResponseBody 中传输它们,任何人都可以帮助我
为 JSOBObject 创建一个 JAVA 对象,反之亦然。我有
所有 POM 依赖项,以及在 servlet-context 中设置的 messageConvertors。
控制器代码当前:
//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;
}
我希望我的问题很清楚,如果有任何疑问,请告诉我。非常感谢。
看看Google's Gson。将对象转换为 JSON 非常简洁 API。您可以通过将 类 中的 @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;
}
没有必要使用@Expose 注释属性,但如果您最终有任何循环引用,它会有所帮助。
祝你好运。
- 我正在开发一个 Android 应用程序,为此我也在开发一个
Spring-基于 MVC 的服务器。不幸的是,在此之前,我还没有做过
在 JSONObjects 上做了那么多工作。目前,我可以发送 Java
从 Android 应用程序向服务器发送对象,并接收 Java 个对象
也。
- 我有兴趣使用 Google 提供的 Volley 框架, 这将避免 Asynctask 的麻烦并且效率更高,但是 它处理 JSONObject。
- 不幸的是,无论我在网上看什么,我都找到了代码 创建 JSOnObjects 以将其保存在本地硬盘驱动器上的某个文件中,但是 不,我想在 ResponseBody 中传输它们,任何人都可以帮助我 为 JSOBObject 创建一个 JAVA 对象,反之亦然。我有 所有 POM 依赖项,以及在 servlet-context 中设置的 messageConvertors。
控制器代码当前:
//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;
}
我希望我的问题很清楚,如果有任何疑问,请告诉我。非常感谢。
看看Google's Gson。将对象转换为 JSON 非常简洁 API。您可以通过将 类 中的 @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;
}
没有必要使用@Expose 注释属性,但如果您最终有任何循环引用,它会有所帮助。
祝你好运。