如何在Android中创建JSON

时间:2014-06-17 10:07:20

标签: android json

如何在Android中创建嵌套JSON。 JSON数据应如下所示:

JSON
Customername = Roshan
Customercode = 100
Customercity = Ernakulam
Customerstate = Kerala
Customercountry = India
Customersales
    Productname = Biscuit
    Productcode = 123
    Producttype = Food
Customersales
    Productname = Shoes
    Productcode = 234
    Producttype = Dress

注意:我能够在不嵌套的情况下创建JSON,但我无法创建嵌套的JSON数据。我们怎么做?

2 个答案:

答案 0 :(得分:1)

您将使用JSONObject。

Link

创建一个JSONObject:

JSONObject json = new JSONObject();

添加元素:

json.accumulate(key,value);

获取JSON:

json.toString;

答案 1 :(得分:0)

好的,我写了你想要的代码。我希望它对其他人也有用。

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

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


    //Populate 4 Customers information for test
    customers = new JSONArray();
    try {
        for (int i = 0; i < 4; i++) {
            //create new customer
            customer = new JSONObject();
            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 = new ArrayList<JSONObject>();
            for (int j = 0; j < 3; j++) {
                customerSales = new JSONObject();
                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 information
        try {
                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 = new JSONArray(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();
        }