簡體   English   中英

java轉換為json對象

[英]java convert to json object

我在SpringMVC框架上工作。 我有JSonController這樣的返回String對象

@RequestMapping(value = "/getOrder/{username}", method = RequestMethod.GET)
@ResponseBody
public String getOrderByDeliverymanUsername(@PathVariable("username") String s) throws JSONException {
    JSONObject orderJsonObject = new JSONObject();
    JSONObject productJsonObject = new JSONObject();
    JSONObject restaurantJsonObject = new JSONObject();

    JSONArray restaurantArray = new JSONArray();
    JSONArray productArray = new JSONArray();

        orderJsonObject.put("customerLatitude", orderService.getOrderByDeliverymanUsername(s).getOrderLat());
        orderJsonObject.put("customerLongitude", orderService.getOrderByDeliverymanUsername(s).getOrderLon());
        orderJsonObject.put("customerName", orderService.getOrderByDeliverymanUsername(s).getMember().getMemberName());
        orderJsonObject.put("orderDescription", orderService.getOrderByDeliverymanUsername(s).getOrderAddressDescription());
        orderJsonObject.put("totalprice", orderService.getOrderByDeliverymanUsername(s).getTotalPrice());


        List<Restaurant> restaurantList = new ArrayList<Restaurant>();
        for (Restaurant restaurant : orderService.getOrderByDeliverymanUsername(s).getRestaurants()) {
            restaurantList.add(restaurant);
        }

       for (Restaurant restaurant : restaurantList) {
            try {
                restaurantJsonObject.put("restaurantID", restaurant.getId());
                restaurantJsonObject.put("restaurantName", restaurant.getRestaurantName());
                restaurantJsonObject.put("restaurantLatitude", restaurant.getRestaurantLat());
                restaurantJsonObject.put("restaurantLongitude", restaurant.getRestaurantLon());
            } catch (NullPointerException e){
                restaurantJsonObject.put("restaurantID", 0);
                restaurantJsonObject.put("restaurantName", 0);
                restaurantJsonObject.put("restaurantLatitude", 0);
                restaurantJsonObject.put("restaurantLongitude", 0);
            }
            restaurantArray.put(restaurantJsonObject);
        }

        for(ProductInCart product : orderService.getOrderByDeliverymanUsername(s).getProductsInCart()){
            productJsonObject.put("productName",product.getProduct().getProductName());
            productJsonObject.put("productPrice",product.getProduct().getProductPrice());
            productArray.put(productJsonObject);
        }
        orderJsonObject.put("products", productArray);
        orderJsonObject.put("restaurants", restaurantArray);

    return orderJsonObject.toString();
}

我不知道為什么restaurantArray和productArray中的對象重復。 例如

{
    customerName : "Pitak",
    totalprice : 4863,
    orderDescription : "some where",
    restaurants : [{
            restaurantLatitude : 33.1414,
            restaurantID : 3,
            restaurantLongitude : 44.5555,
            restaurantName : "Nong hoi"
        }, {
            restaurantLatitude : 33.1414,
            restaurantID : 3,
            restaurantLongitude : 44.5555,
            restaurantName : "Nong hoi"
        }
    ],
    products : [{
            productPrice : 35,
            productName : "Khao kha mu"
        }, {
            productPrice : 35,
            productName : "Khao kha mu"
        }, {
            productPrice : 35,
            productName : "Khao kha mu"
        }, {
            productPrice : 35,
            productName : "Khao kha mu"
        }
    ],
    customerLatitude : 66.0956,
    customerLongitude : 88.45671
}

謝謝大家,對不起我的英語。

解決!! 我只是將JsonObject移入循環

@RequestMapping(value = "/getOrder/{username}", method = RequestMethod.GET)
    @ResponseBody
    public String getOrderByDeliverymanUsername(@PathVariable("username") String s) throws JSONException {
        JSONObject orderJsonObject = new JSONObject();
        JSONArray restaurantArray = new JSONArray();
        JSONArray productArray = new JSONArray();
        orderJsonObject.put("customerLatitude", orderService.getOrderByDeliverymanUsername(s).getOrderLat());
        orderJsonObject.put("customerLongitude", orderService.getOrderByDeliverymanUsername(s).getOrderLon());
        orderJsonObject.put("customerName", orderService.getOrderByDeliverymanUsername(s).getMember().getMemberName());
        orderJsonObject.put("orderDescription", orderService.getOrderByDeliverymanUsername(s).getOrderAddressDescription());
        orderJsonObject.put("totalprice", orderService.getOrderByDeliverymanUsername(s).getTotalPrice());


        List<Restaurant> restaurantList = new ArrayList<Restaurant>();
        for (Restaurant restaurant : orderService.getOrderByDeliverymanUsername(s).getRestaurants()) {
            restaurantList.add(restaurant);
        }

       for (Restaurant restaurant : restaurantList) {
           JSONObject restaurantJsonObject = new JSONObject();
            try {
                restaurantJsonObject.put("restaurantID", restaurant.getId());
                restaurantJsonObject.put("restaurantName", restaurant.getRestaurantName());
                restaurantJsonObject.put("restaurantLatitude", restaurant.getRestaurantLat());
                restaurantJsonObject.put("restaurantLongitude", restaurant.getRestaurantLon());
            } catch (NullPointerException e){
                restaurantJsonObject.put("restaurantID", 0);
                restaurantJsonObject.put("restaurantName", 0);
                restaurantJsonObject.put("restaurantLatitude", 0);
                restaurantJsonObject.put("restaurantLongitude", 0);
            }
            restaurantArray.put(restaurantJsonObject);
        }

        for(ProductInCart product : orderService.getOrderByDeliverymanUsername(s).getProductsInCart()){
            JSONObject productJsonObject = new JSONObject();
            productJsonObject.put("productName",product.getProduct().getProductName());
            productJsonObject.put("productPrice",product.getProduct().getProductPrice());
            productArray.put(productJsonObject);
        }
        orderJsonObject.put("products", productArray);
        orderJsonObject.put("restaurants", restaurantArray);

    return orderJsonObject.toString();
}

再次感謝大家。

當您這樣做時:

for (Restaurant restaurant : restaurantList)

它生成一個迭代器並通過它循環。 傳遞引用時,它將當前值存儲在數組中,即迭代器中的最后一個值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM