Skip to content Skip to sidebar Skip to footer

How To Create Json In Android

How do I create nested JSON in Android. The JSON data should look like this : JSON Customername = Roshan Customercode = 100 Customercity = Ernakulam Customerstate = Kerala Customer

Solution 1:

You would work with JSONObject.

Link

Create a JSONObject:

JSONObject json = newJSONObject();

Add element:

json.accumulate(key,value);

Get a JSON:

json.toString;

Solution 2:

OK, I wrote the code you wanted. I hope it be useful for others too.

TextView testText;
    testText = (TextView) findViewById(R.id.testText);

    JSONObject customer,customerSales;
    JSONArray customers;
    List<JSONObject> productList;


    //Populate 4 Customers information for test
    customers = newJSONArray();
    try {
        for (int i = 0; i < 4; i++) {
            //create new customer
            customer = newJSONObject();
            customer.put("Customername", "name "+(i+1));
            customer.put("Customercode", 100+i+1);
            customer.put("Customercity", "city "+(i+1));
            customer.put("Customerstate", "state "+(i+1));
            customer.put("Customercountry", "country "+(i+1));

            //Add 3 Product Sales for each customer
            productList = newArrayList<JSONObject>();
            for (int j = 0; j < 3; j++) {
                customerSales = newJSONObject();
                customerSales.put("Productname", "Product name "+(j+1));
                customerSales.put("Productcode", 200+j+1);
                customerSales.put("Producttype", "Product type "+(j+1));
                productList.add(customerSales);
            }

            customer.put("Customersales", productList);
            customers.put(customer);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    //Access Customers informationtry {
                testText.setText("");
            for (int i = 0; i < customers.length(); i++) {

                testText.append("Costumer number "+ (i+1) + ":\n");
                testText.append("    Customer Name:"+customers.getJSONObject(i).getString("Customername"));
                testText.append("\n");
                testText.append("    Customer Code:"+customers.getJSONObject(i).getInt("Customercode")+"");
                testText.append("\n");
                testText.append("    Customer City:"+customers.getJSONObject(i).getString("Customercity"));
                testText.append("\n");
                testText.append("    Customer State:"+customers.getJSONObject(i).getString("Customerstate"));
                testText.append("\n");
                testText.append("    Customer Country:"+customers.getJSONObject(i).getString("Customercountry"));
                testText.append("\n    Sales for this Customer:\n");

                JSONArray tmpArray = newJSONArray(customers.getJSONObject(i).getString("Customersales"));


                for (int j = 0; j < tmpArray.length(); j++) {
                    testText.append("        Product number "+(j+1)+":\n");         
                    testText.append("            Product Name:"+tmpArray.getJSONObject(j).getString("Productname"));
                    testText.append("\n");
                    testText.append("            Product Code:"+tmpArray.getJSONObject(j).getInt("Productcode")+"");
                    testText.append("\n");
                    testText.append("            Product Type:"+tmpArray.getJSONObject(j).getString("Producttype"));
                    testText.append("\n");
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

Post a Comment for "How To Create Json In Android"