Example usage for org.json JSONObject toString

List of usage examples for org.json JSONObject toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Make a JSON text of this JSONObject.

Usage

From source file:au.com.borner.salesforce.client.rest.domain.ContainerAsyncRequest.java

public List<CompilerError> getCompilerErrors() {
    // CompilerErrors is a JSON Array wrapped in a String ..... :-(
    String compilerErrors = getString("CompilerErrors");
    JSONArray jsonArray = new JSONArray(compilerErrors);
    List<CompilerError> result = new ArrayList<CompilerError>(jsonArray.length());
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        result.add(new CompilerError(jsonObject.toString()));
    }// w w  w.  j a v  a2  s.  c  o  m
    return result;
}

From source file:abelymiguel.miralaprima.GetPrima.java

/**
 * Processes requests for both HTTP//  w  w w  .  ja  va 2s. c  om
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        _con = Utils.getConnection();
        _stmt = _con.createStatement();

    } catch (URISyntaxException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    }
    PrintWriter out = response.getWriter();
    response.setContentType("text/javascript;charset=UTF-8");

    String country_code;
    country_code = request.getParameter("country_code");

    JSONObject jsonObject;
    JSONArray jsonArray;
    String json_str;
    if (country_code != null) {
        jsonObject = new JSONObject(this.getCountry(country_code));
        json_str = jsonObject.toString();
    } else {
        jsonArray = new JSONArray(this.getAllCountries());
        json_str = jsonArray.toString();
    }

    String jsonpCallback = request.getParameter("callback");
    if (jsonpCallback != null) {
        out.write(jsonpCallback + "(" + json_str + ")");
    } else {
        out.println(json_str);
    }
    try {
        _con.close();
        _stmt.close();
        _rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.close();
}

From source file:au.com.borner.salesforce.client.rest.domain.AbstractJSONObject.java

@SuppressWarnings("unchecked")
protected <T extends AbstractJSONObject> List<T> getChildEntities(String name, Class<T> resultClass) {
    try {// w ww .j  a va 2s  .c  o  m
        Constructor<T> constructor = resultClass.getConstructor(String.class);
        JSONArray children = getJSONArray(name);
        List<T> result = new ArrayList<T>(children.length());
        for (int i = 0; i < children.length(); i++) {
            JSONObject jsonObject = children.getJSONObject(i);
            T instance = constructor.newInstance(jsonObject.toString()); // is there any way to avoid double serialisation?
            result.add(instance);
        }
        return result;
    } catch (Exception e) {
        throw new RuntimeException("Error parsing child elements", e);
    }
}

From source file:abelymiguel.miralaprima.SetMoza.java

/**
 * Processes requests for both HTTP//from  w w  w.  ja  va2s .  c o m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    try {
        _con = Utils.getConnection();
        _stmt = _con.createStatement();
    } catch (URISyntaxException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }

    PrintWriter out = null;
    try {
        out = response.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }
    response.setContentType("text/javascript;charset=UTF-8");

    String url_moza;
    url_moza = request.getParameter("url_moza");

    String country_code;
    country_code = request.getParameter("country_code");

    if (country_code == null) {
        country_code = "ALL";
    }

    JSONObject jsonObject;
    String json_str;

    jsonObject = new JSONObject(this.setMozaInDB(url_moza, country_code));
    json_str = jsonObject.toString();

    String jsonpCallback = request.getParameter("callback");
    if (jsonpCallback != null) {
        out.write(jsonpCallback + "(" + json_str + ")");
    } else {
        out.println(json_str);
    }
    try {
        _con.close();
        _stmt.close();
    } catch (SQLException ex) {
        Logger.getLogger(SetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.close();
}

From source file:com.google.cast.samples.tictactoe.GameChannel.java

/**
 * Attempts to connect to an existing session of the game by sending a join command.
 *
 * @param name the name of the player that is joining
 *///from  w ww  .ja v  a2 s  . c  om
public final void join(GoogleApiClient apiClient, String name) {
    try {
        Log.d(TAG, "join: " + name);
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, KEY_JOIN);
        payload.put(KEY_NAME, name);
        sendMessage(apiClient, payload.toString());
    } catch (JSONException e) {
        Log.e(TAG, "Cannot create object to join a game", e);
    }
}

From source file:com.google.cast.samples.tictactoe.GameChannel.java

/**
 * Attempts to make a move by sending a command to place a piece in the given row and column.
 *//* w w  w  .j  av  a 2 s  . c  o  m*/
public final void move(GoogleApiClient apiClient, final int row, final int column) {
    Log.d(TAG, "move: row:" + row + " column:" + column);
    try {
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, KEY_MOVE);
        payload.put(KEY_ROW, row);
        payload.put(KEY_COLUMN, column);
        sendMessage(apiClient, payload.toString());
    } catch (JSONException e) {
        Log.e(TAG, "Cannot create object to send a move", e);
    }
}

From source file:com.google.cast.samples.tictactoe.GameChannel.java

/**
 * Sends a command to leave the current game.
 *//*from  w  w  w .j  a v a 2 s  .  c  o  m*/
public final void leave(GoogleApiClient apiClient) {
    try {
        Log.d(TAG, "leave");
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, KEY_LEAVE);
        sendMessage(apiClient, payload.toString());
    } catch (JSONException e) {
        Log.e(TAG, "Cannot create object to leave a game", e);
    }
}

From source file:com.google.cast.samples.tictactoe.GameChannel.java

/**
 * Sends a command requesting the current layout of the board.
 *///ww w. j  a  v a  2s. c  om
public final void requestBoardLayout(GoogleApiClient apiClient) {
    try {
        Log.d(TAG, "requestBoardLayout");
        JSONObject payload = new JSONObject();
        payload.put(KEY_COMMAND, KEY_BOARD_LAYOUT_REQUEST);
        sendMessage(apiClient, payload.toString());
    } catch (JSONException e) {
        Log.e(TAG, "Cannot create object to request board layout", e);
    }
}

From source file:com.example.cuisoap.agrimac.machineRegister.containerActivity.java

@Override
public void onButtonClicked(JSONObject data, int from) {
    System.out.println(data.toString());
    switch (from) {
    case 1://from  w ww .ja  v  a 2s  .c  o m
        try {
            send_data.put("drive_type", data.getString("drive_type"));
            send_data.put("driver_name", data.getString("driver_name"));
            send_data.put("driver_age", data.getString("driver_age"));
            send_data.put("driver_gender", data.getString("driver_gender"));
            send_data.put("driver_license_type", data.getString("license_type"));
            //send_data.put("driver_license",data.getString("license_path"));
            filelist.put("driver_license", data.getString("license_path"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mPager.setCurrentItem(1);
        break;
    case 2:
        try {
            send_data.put("machine_type", data.getString("machine_type"));
            send_data.put("machine_name", data.getString("machine_name"));
            send_data.put("machine_power", data.getString("machine_power"));
            send_data.put("passenger_num", data.getString("passenger_num"));
            send_data.put("machine_wheeldistance", data.getString("wheel_distance"));
            send_data.put("machine_checktime", data.getString("check_time"));
            send_data.put("machine_paytype", data.getString("pay_type"));
            send_data.put("machine_powertype", data.getString("power"));
            //  send_data.put("machine_license1",data.getString("license_path1"));
            //  send_data.put("machine_license2",data.getString("license_path2"));
            filelist.put("machine_license1", data.getString("license_path1"));
            filelist.put("machine_license2", data.getString("license_path2"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mPager.setCurrentItem(2);
        break;
    case 3:
        try {
            send_data.put("lease_month", data.getString("lease_month"));
            send_data.put("lease_time", data.getString("lease_time"));
            send_data.put("work_condition", data.getString("work_condition"));
            send_data.put("need_type", data.getString("need_type"));
            if (data.getString("need_type").equals("2"))
                send_data.put("need_item", data.getString("needItem"));
            send_data.put("house_type", data.getString("house_type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        /* Message e=Message.obtain();
        JSONObject o=new JSONObject();
        try {
            o.put("status","0");
            o.put("id","1");
            e.obj=o.toString();
            e.setTarget(h);
            e.sendToTarget();
        } catch (JSONException e1) {
            e1.printStackTrace();
        }*/
        new Thread(send).start();

        //TODO ?
    }

}

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 {/* w  w  w. j a v  a2s. c  o  m*/
        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();

}