Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

In this page you can find the example usage for org.json JSONObject put.

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:com.graphhopper.http.GraphHopperServlet.java

Object createPoints(PointList points, boolean pointsEncoded, boolean includeElevation) throws JSONException {
    if (pointsEncoded)
        return WebHelper.encodePolyline(points, includeElevation);

    JSONObject jsonPoints = new JSONObject();
    jsonPoints.put("type", "LineString");
    jsonPoints.put("coordinates", points.toGeoJson(includeElevation));
    return jsonPoints;
}

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

@RequestMapping(value = "quote/add", method = RequestMethod.GET)
public @ResponseBody String addCart(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {
    //      String jstr = "{\"cartId\":-1, \"productId\":2, \"attributeId\":1, \"quantity\":2}";
    //      String jstr = "{\"cartId\":1, \"productId\":3, \"attributeId\":1, \"quantity\":2}";

    int cartId = -1;
    int productId = -1;
    int attributeId = -1;
    int quantity = -1;
    try {/*ww  w . java2  s . c  om*/
        cartId = jsonParams.getInt("cartId");
        productId = jsonParams.getInt("productId");
        attributeId = jsonParams.getInt("attributeId");
        quantity = jsonParams.getInt("quantity");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    //      cartId = 1;
    //      productId = 2;
    //      attributeId = 1;
    //      quantity = 2;

    CheckoutAddToCart checkoutAddToCart = new CheckoutAddToCart();
    checkoutAddToCart.setCartId(cartId);
    checkoutAddToCart.setProductId(productId);
    checkoutAddToCart.setAttributeId(attributeId);
    checkoutAddToCart.setQuantity(quantity);

    //Making entry to CheckoutQuote
    CheckoutQuote checkoutQuote = new CheckoutQuote();
    checkoutQuote.setId(checkoutAddToCart.getCartId());
    int insertedCartId = -1;
    if (checkoutQuote.getId() == -1) {
        System.out.println("Setting create id");
        checkoutQuote.setId(0);
        checkoutQuote.setCreatedAt(Utilities.getCurrentDateTime());
        checkoutQuote.setBaseGrandTotal(0);
        checkoutQuote.setBaseSubtotal(0);
        checkoutQuote.setGrandTotal(0);
        checkoutQuote.setSubtotal(0);
        checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime());
        this.checkoutQuoteService.addCheckoutQuote(checkoutQuote);
    } else {
        checkoutQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId);
        //         checkoutQuote.setCreatedAt(this.checkoutQuoteService.getCheckoutQuoteById(checkoutQuote.getId()).getCreatedAt());
    }
    insertedCartId = checkoutQuote.getId();
    System.out.println("CheckoutQuote ID: insertedCartId : " + insertedCartId);

    //Making entry to CheckoutQuoteItem
    ProductsMaster productItem = getJVoidProduct(checkoutAddToCart.getProductId());

    CheckoutQuoteItem checkoutQuoteItem = this.checkoutQuoteItemService.getCheckoutQuoteItem(cartId, productId);
    int addingNew = 0;
    if (checkoutQuoteItem == null) {
        addingNew = 1;
        checkoutQuoteItem = new CheckoutQuoteItem();
        checkoutQuote.setItemsCount(checkoutQuote.getItemsCount() + 1);
        checkoutQuoteItem.setCreatedAt(Utilities.getCurrentDateTime());
    }

    checkoutQuoteItem.setWeight(productItem.getWeight());
    checkoutQuoteItem.setQuantity(checkoutQuoteItem.getQuantity() + checkoutAddToCart.getQuantity());
    checkoutQuoteItem.setSku(productItem.getSku());
    checkoutQuoteItem.setPrice(productItem.getPrice());
    checkoutQuoteItem.setBasePrice(productItem.getPrice());
    checkoutQuoteItem.setDescription(productItem.getDescription());
    checkoutQuoteItem.setName(productItem.getName());
    checkoutQuoteItem.setProductId(productItem.getId());
    checkoutQuoteItem.setQuoteId(insertedCartId);

    //      if (checkoutAddToCart.getCartId() == -1) {
    checkoutQuote.setItemsQuantity(checkoutQuote.getItemsQuantity() + checkoutAddToCart.getQuantity());
    //      }
    //      else {
    //         checkoutQuoteItem.setCreatedAt(this.checkoutQuoteItemService.getCheckoutQuoteItemById(checkoutQuoteItem.getId()).getCreatedAt());
    //      }
    checkoutQuoteItem.setRowTotal(checkoutQuoteItem.getPrice() * checkoutQuoteItem.getQuantity());
    checkoutQuoteItem.setBaseRowTotal(checkoutQuoteItem.getBasePrice() * checkoutQuoteItem.getQuantity());
    checkoutQuoteItem.setRowWeight(checkoutQuoteItem.getWeight() * checkoutQuoteItem.getQuantity());
    checkoutQuoteItem.setUpdatedAt(Utilities.getCurrentDateTime());

    this.checkoutQuoteItemService.addCheckoutQuoteItem(checkoutQuoteItem);
    int insertedProductId = checkoutQuoteItem.getId();
    System.out.println("CheckoutItem ID: insertedProductId : " + insertedProductId);

    System.out.println("Abhi checkoutquote b4 = " + checkoutQuote.toString());

    if (addingNew == 0) {
        checkoutQuote.setBaseSubtotal(checkoutQuote.getBaseSubtotal()
                + checkoutQuoteItem.getBasePrice() * checkoutAddToCart.getQuantity());
        checkoutQuote.setSubtotal(
                checkoutQuote.getSubtotal() + checkoutQuoteItem.getPrice() * checkoutAddToCart.getQuantity());
    } else {
        checkoutQuote.setBaseSubtotal(checkoutQuote.getBaseSubtotal() + checkoutQuoteItem.getBaseRowTotal());
        checkoutQuote.setSubtotal(checkoutQuote.getSubtotal() + checkoutQuoteItem.getRowTotal());
    }

    checkoutQuote.setBaseGrandTotal(checkoutQuote.getBaseSubtotal() + 50);
    checkoutQuote.setGrandTotal(checkoutQuote.getSubtotal() + 50);
    checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime());

    this.checkoutQuoteService.addCheckoutQuote(checkoutQuote);
    System.out.println("Abhi checkoutquote after = " + checkoutQuote.toString());
    //      }

    JSONObject jsonObj = new JSONObject();
    try {
        jsonObj.put("cartId", insertedCartId);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonObj.toString();

}

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

public ProductsMaster getJVoidProduct(int productId) {

    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);

    JSONObject jsonObj = new JSONObject();
    try {// w  w  w.  j  av  a2 s  . c  o m
        jsonObj.put("id", productId);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("param jsonObj=>" + jsonObj.toString());

    UriComponentsBuilder builder = UriComponentsBuilder
            .fromHttpUrl(PRODUCT_SERVER_URI + URIConstants.GET_PRODUCT).queryParam("params", jsonObj);
    HttpEntity<?> entity = new HttpEntity<>(headers);
    HttpEntity<String> returnString = restTemplate.exchange(builder.build().toUri(), HttpMethod.GET, entity,
            String.class);
    System.out.println("returnString=>" + returnString);

    JSONObject returnJsonObj = null;
    try {
        returnJsonObj = new JSONObject(returnString.getBody());
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONArray productsArr = null;
    ProductsMaster productsMaster = null;
    try {
        productsArr = returnJsonObj.getJSONArray("products");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    productsMaster = new ProductsMaster();
    try {
        ObjectMapper mapper = new ObjectMapper();
        try {
            productsMaster = mapper.readValue(productsArr.getJSONObject(0).toString(), ProductsMaster.class);
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return productsMaster;
}

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

@RequestMapping(value = "quote/cart", method = RequestMethod.GET)
public @ResponseBody String getCart(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    //System.out.println("TOTAL RECS1:"+this.productsMasterService.getAllProducts().size());

    int cartId = -1;
    try {/*from   w  w w . j  av a2 s.  c o m*/
        cartId = jsonParams.getInt("cartId");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JSONObject cartObject = new JSONObject();
    JSONArray quoteItems = new JSONArray();

    List<CheckoutQuoteItem> quoteItemsList = null;
    quoteItemsList = this.checkoutQuoteItemService.listCheckoutQuoteItems(cartId);
    System.out.println("ABHI Quote Item List Count= " + quoteItemsList.size());
    ObjectMapper mapper = new ObjectMapper();
    for (int i = 0; i < quoteItemsList.size(); i++) {

        try {
            String strQuoteItemObj = mapper.writeValueAsString(quoteItemsList.get(i));
            JSONObject jsonObj = new JSONObject(strQuoteItemObj);

            quoteItems.put(jsonObj);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    CheckoutQuote currentQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId);

    try {
        cartObject.put("items", quoteItems);
        cartObject.put("total", currentQuote.getGrandTotal());
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    return cartObject.toString();

}

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

@RequestMapping(value = "quote/delete", method = RequestMethod.GET)
public @ResponseBody String deleteCart(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {
    //      String jstr = "{\"cartId\":-1, \"productId\":2, \"attributeId\":1, \"quantity\":2}";
    //      String jstr = "{\"cartId\":1, \"productId\":3, \"attributeId\":1, \"quantity\":2}";
    System.out.println("Inside delete");
    int cartId = -1;
    int productId = -1;
    try {//from  w  w w .  j  av a2 s. c  o m
        cartId = jsonParams.getInt("cartId");
        productId = jsonParams.getInt("productId");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    //      cartId = 1;
    //      productId = -1;

    CheckoutQuoteItem quoteItemToDelete = null;
    CheckoutQuote checkoutQuote = new CheckoutQuote();
    List<CheckoutQuoteItem> quoteItemsList = null;
    if (productId != -1) {
        quoteItemToDelete = this.checkoutQuoteItemService.getCheckoutQuoteItem(cartId, productId);
        quoteItemsList = new ArrayList<CheckoutQuoteItem>();
        quoteItemsList.add(quoteItemToDelete);
    } else {
        quoteItemsList = this.checkoutQuoteItemService.listCheckoutQuoteItems(cartId);
        System.out.println("ABHI Quote Item List Count= " + quoteItemsList.size());
    }

    checkoutQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId);
    for (int i = 0; i < quoteItemsList.size(); i++) {
        CheckoutQuoteItem currentQuoteItemToDelete = quoteItemsList.get(i);

        checkoutQuote.setItemsCount(checkoutQuote.getItemsCount() - 1);
        checkoutQuote
                .setItemsQuantity(checkoutQuote.getItemsQuantity() - currentQuoteItemToDelete.getQuantity());
        checkoutQuote
                .setBaseSubtotal(checkoutQuote.getBaseSubtotal() - currentQuoteItemToDelete.getBaseRowTotal());
        checkoutQuote.setSubtotal(checkoutQuote.getSubtotal() - currentQuoteItemToDelete.getRowTotal());
        this.checkoutQuoteItemService.removeCheckoutQuoteItem(currentQuoteItemToDelete.getId());
    }
    checkoutQuote.setBaseGrandTotal(checkoutQuote.getBaseSubtotal() + 50);
    checkoutQuote.setGrandTotal(checkoutQuote.getSubtotal() + 50);
    checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime());
    System.out.println("Abhi checkoutquote after = " + checkoutQuote.toString());

    int returnCartId = cartId;
    if (productId == -1) {
        this.checkoutQuoteService.removeCheckoutQuote(checkoutQuote.getId());
        returnCartId = -1;
    } else {
        this.checkoutQuoteService.addCheckoutQuote(checkoutQuote);
    }

    JSONObject jsonObj = new JSONObject();
    try {
        jsonObj.put("cartId", returnCartId);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonObj.toString();

}

From source file:com.jvoid.quote.controller.JVoidQuoteController.java

@RequestMapping(value = "quote/addaddress", method = RequestMethod.GET)
public @ResponseBody String addAddress(@RequestParam(required = false, value = "callback") String callback,
        @RequestParam(required = false, value = "params") JSONObject jsonParams, HttpServletResponse response) {

    int cartId = -1;
    JSONObject user = null;/*from   www.ja va2s. c  om*/
    JSONObject billing = null;
    JSONObject shipping = null;
    String checkoutMethod = null;
    try {
        cartId = jsonParams.getInt("cartId");
        user = jsonParams.getJSONObject("user");
        billing = jsonParams.getJSONObject("billing");
        shipping = jsonParams.getJSONObject("shipping");
        checkoutMethod = jsonParams.getString("checkoutMethod");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    CheckoutQuote checkoutQuote = this.checkoutQuoteService.getCheckoutQuoteById(cartId);
    checkoutQuote.setCheckoutMethod(checkoutMethod);
    checkoutQuote.setUpdatedAt(Utilities.getCurrentDateTime());
    this.checkoutQuoteService.updateCheckoutQuote(checkoutQuote);

    try {
        CheckoutQuoteAddress checkoutQuoteAddress = new CheckoutQuoteAddress();
        checkoutQuoteAddress.setQuoteId(cartId);

        checkoutQuoteAddress.setFirstname(user.getString("firstName"));
        checkoutQuoteAddress.setLastname(user.getString("lastName"));
        checkoutQuoteAddress.setCompany(user.getString("company"));
        checkoutQuoteAddress.setEmail(user.getString("emailAddress"));

        checkoutQuoteAddress.setAddressType("billing");
        checkoutQuoteAddress
                .setStreet(billing.getString("address") + "," + billing.getString("streetAddress2"));
        checkoutQuoteAddress.setCity(billing.getString("city") + "," + billing.getString("state"));
        checkoutQuoteAddress.setCountryId(billing.getString("country"));
        checkoutQuoteAddress.setPostcode(billing.getString("zip"));
        checkoutQuoteAddress.setTelephone(billing.getString("telephone"));
        checkoutQuoteAddress.setFax(billing.getString("fax"));

        checkoutQuoteAddress.setCreatedAt(Utilities.getCurrentDateTime());
        checkoutQuoteAddress.setUpdatedAt(Utilities.getCurrentDateTime());

        this.checkoutQuoteAddressService.addCheckoutQuoteAddress(checkoutQuoteAddress);

        checkoutQuoteAddress = null;
        checkoutQuoteAddress = new CheckoutQuoteAddress();
        checkoutQuoteAddress.setQuoteId(cartId);

        checkoutQuoteAddress.setFirstname(user.getString("firstName"));
        checkoutQuoteAddress.setLastname(user.getString("lastName"));
        checkoutQuoteAddress.setCompany(user.getString("company"));
        checkoutQuoteAddress.setEmail(user.getString("emailAddress"));

        checkoutQuoteAddress.setAddressType("shipping");
        checkoutQuoteAddress
                .setStreet(shipping.getString("address") + "," + shipping.getString("streetAddress2"));
        checkoutQuoteAddress.setCity(shipping.getString("city") + "," + shipping.getString("state"));
        checkoutQuoteAddress.setCountryId(shipping.getString("country"));
        checkoutQuoteAddress.setPostcode(shipping.getString("zip"));
        checkoutQuoteAddress.setTelephone(shipping.getString("telephone"));
        checkoutQuoteAddress.setFax(shipping.getString("fax"));

        checkoutQuoteAddress.setCreatedAt(Utilities.getCurrentDateTime());
        checkoutQuoteAddress.setUpdatedAt(Utilities.getCurrentDateTime());

        this.checkoutQuoteAddressService.addCheckoutQuoteAddress(checkoutQuoteAddress);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONObject jsonObj = new JSONObject();
    try {
        jsonObj.put("cartId", cartId);
        jsonObj.put("result", "Success");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return jsonObj.toString();
}

From source file:org.b3log.latke.servlet.converter.Converters.java

@Override
public Object convert(final Class<?> parameterType, final String paramterName, final HTTPRequestContext context,
        final MatchResult result, final int sequence) throws Exception {

    final JSONObject ret = new JSONObject();

    final HttpServletRequest request = context.getRequest();

    for (Object o : request.getParameterMap().keySet()) {

        ret.put(String.valueOf(o), request.getParameterMap().get(o));
    }//from   w w  w  . j  a v a 2 s  .  c  o  m
    // mapValue will cover
    for (String key : result.getMapValues().keySet()) {
        ret.put(key, result.getMapValues().get(key));
    }
    return ret;
}

From source file:com.intel.iotkitlib.LibModules.AccountManagement.java

private String createBodyForAddingUserToAccount(String accountId, String inviteeUserId, Boolean isAdmin)
        throws JSONException {
    JSONObject addUserJson = new JSONObject();
    addUserJson.put("id", inviteeUserId);
    JSONObject accountsJson = new JSONObject();
    if (isAdmin) {
        accountsJson.put(accountId, "admin");
    } else {/*from www.j  a v a  2 s .  c  o  m*/
        accountsJson.put(accountId, "user");
    }
    addUserJson.put("accounts", accountsJson);
    return addUserJson.toString();
}

From source file:com.commontime.plugin.LocationManager.java

private void createMonitorCallbacks(final CallbackContext callbackContext) {

    //Monitor callbacks
    iBeaconManager.setMonitorNotifier(new MonitorNotifier() {
        @Override//from w  ww  .  j  a  v a2  s  .c  o m
        public void didEnterRegion(Region region) {
            debugLog("didEnterRegion INSIDE for " + region.getUniqueId());
            dispatchMonitorState("didEnterRegion", MonitorNotifier.INSIDE, region, callbackContext);
        }

        @Override
        public void didExitRegion(Region region) {
            debugLog("didExitRegion OUTSIDE for " + region.getUniqueId());
            dispatchMonitorState("didExitRegion", MonitorNotifier.OUTSIDE, region, callbackContext);
        }

        @Override
        public void didDetermineStateForRegion(int state, Region region) {
            debugLog("didDetermineStateForRegion '" + nameOfRegionState(state) + "' for region: "
                    + region.getUniqueId());
            dispatchMonitorState("didDetermineStateForRegion", state, region, callbackContext);
        }

        // Send state to JS callback until told to stop
        private void dispatchMonitorState(final String eventType, final int state, final Region region,
                final CallbackContext callbackContext) {

            threadPoolExecutor.execute(new Runnable() {
                public void run() {
                    try {
                        JSONObject data = new JSONObject();
                        data.put("eventType", eventType);
                        data.put("region", mapOfRegion(region));

                        if (eventType.equals("didDetermineStateForRegion")) {
                            String stateName = nameOfRegionState(state);
                            data.put("state", stateName);
                        }
                        //send and keep reference to callback 
                        PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    } catch (Exception e) {
                        Log.e(TAG, "'monitoringDidFailForRegion' exception " + e.getCause());
                        beaconServiceNotifier.monitoringDidFailForRegion(region, e);

                    }
                }
            });
        }
    });

}

From source file:com.commontime.plugin.LocationManager.java

private void createRangingCallbacks(final CallbackContext callbackContext) {

    iBeaconManager.setRangeNotifier(new RangeNotifier() {
        @Override/*from w ww.j  a v a2 s . com*/
        public void didRangeBeaconsInRegion(final Collection<Beacon> iBeacons, final Region region) {

            threadPoolExecutor.execute(new Runnable() {
                public void run() {

                    try {
                        JSONObject data = new JSONObject();
                        JSONArray beaconData = new JSONArray();
                        for (Beacon beacon : iBeacons) {
                            beaconData.put(mapOfBeacon(beacon));
                        }
                        data.put("eventType", "didRangeBeaconsInRegion");
                        data.put("region", mapOfRegion(region));
                        data.put("beacons", beaconData);

                        debugLog("didRangeBeacons: " + data.toString());

                        //send and keep reference to callback 
                        PluginResult result = new PluginResult(PluginResult.Status.OK, data);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);

                    } catch (Exception e) {
                        Log.e(TAG, "'rangingBeaconsDidFailForRegion' exception " + e.getCause());
                        beaconServiceNotifier.rangingBeaconsDidFailForRegion(region, e);
                    }
                }
            });
        }

    });

}