国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

將Java對象轉換為JSONObject并以GET方法傳輸

瀏覽:113日期:2024-05-13 09:24:59
(adsbygoogle = window.adsbygoogle || []).push({}); 如何解決將Java對象轉換為JSONObject并以GET方法傳輸?

看看Google的Gson。這是一個非常簡潔的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;}

不必使用@Expose注釋屬性,但是如果最終有任何循環引用,它將很有用。

祝好運。

解決方法 我正在開發一個Android應用程序,為此我還正在開發基于Spring-MVC的服務器。不幸的是,在此之前,我還沒有在JSONObjects上做太多工作。目前,我能夠從Android應用程序將Java對象發送到服務器,也可以接收Java對象。 我對使用Google提供的Volley框架感興趣,該框架將避免Asynctask的麻煩,并且效率更高,但它處理JSONObject。不幸的是,無論我在網上什么地方,都找到了創建JSOnObjects的代碼,將其保存在本地硬盤上的某個文件中,但是不,我想在ResponseBody中傳輸它們,任何人都可以幫助我為JSOBObject創建JAVA對象反之亦然。我具有所有POM依賴項,并在servlet上下文中設置了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; }

我希望我的問題很清楚,如果有任何疑問,請告訴我。非常感謝。

標簽: java
相關文章:
主站蜘蛛池模板: 常宁市| 伊川县| 临安市| 桂阳县| 济阳县| 临沭县| 民勤县| 库伦旗| 南昌县| 佛冈县| 腾冲县| 礼泉县| 荥阳市| 吉安县| 苗栗县| 永春县| 永年县| 偃师市| 青海省| 平果县| 平原县| 会同县| 玉龙| 崇阳县| 舒兰市| 宁强县| 新干县| 汝城县| 杭锦旗| 绿春县| 同心县| 哈巴河县| 商水县| 舒城县| 济阳县| 噶尔县| 肇东市| 阿克陶县| 益阳市| 酉阳| 夏河县|