将Java对象转换为JSONObject并以GET方法传输它. [英] Converting Java object to JSONObject and transmit it at GET method.

查看:490
本文介绍了将Java对象转换为JSONObject并以GET方法传输它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我正在开发一个Android应用程序,为此我还正在开发一个 基于Spring-MVC的服务器.不幸的是,在此之前,我还没有做过 在JSONObjects上做了很多工作.目前,我可以发送Java 对象从Android应用程序发送到服务器,并接收Java对象 也.
    • 我对使用Google提供的Volley框架感兴趣, 这样可以避免Asynctask的麻烦,并且效率更高,但是 它处理JSONObject.
    • 不幸的是,无论我在网上看什么,我都找到了以下代码: 创建JSOnObjects以将其保存在本地硬盘驱动器上的某个文件中,但是 不,我想在ResponseBody中传送它们,有人可以帮我吗 首先创建一个JSOBObject的JAVA对象,反之亦然.我有 所有POM依赖项,以及在Servlet上下文中设置的messageConvertors.
    • I am working on an Android app, for which I am also working on a Spring-MVC based server. Unfortunately before this, I have not done that much work on JSONObjects. Currently, I am able to send Java objects to the server from the Android app, and receive Java objects too.
      • I am interested in using the Volley framework provided by Google, which will avoid the hassle of Asynctask and is more efficient, but it deals with JSONObject.
      • Unfortunately wherever I looked on the net, I found the code to create JSOnObjects to save it in some file on Local Hard drive, but no, I would like to transmit them in ResponseBody, can anyone help me out with creating a JAVA object to JSOBObject and vice-versa. I have all POM dependencies, and messageConvertors set in servlet-context.

      当前控制器代码:

      //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;
          }
      

      我希望我的问题很清楚,如果有任何疑问,请告诉我.非常感谢.

      I hope my question was clear, if there is any doubt, please let me know. Thanks a lot.

      推荐答案

      看看 Google的格森(Gson).这是一个非常简洁的API,用于将对象转换为JSON.通过将类中的@Expose批注添加到需要包括的属性中,可以轻松地指定属性.像这样尝试:

      Take a look at Google's Gson. It's a pretty concise API for converting objects to JSON. You can easily specify properties by adding the @Expose annotation in your classes to the properties you need to include. Try it like this:

      @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注释属性,但如果最终有任何循环引用,这将有所帮助.

      It's not necessary to annotate properties with @Expose but it will help if you end up having any circular references.

      祝你好运.

      这篇关于将Java对象转换为JSONObject并以GET方法传输它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆