如何在Android的创造JSON [英] How to create JSON in Android

查看:171
本文介绍了如何在Android的创造JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何创建Android的嵌套JSON。 JSON数据应该是这样的:

How do I create nested JSON in Android. The JSON data should look like this :

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数据。我们如何做到这一点?

Note : I was able to create JSON without nesting, but I am not able to create nested JSON data. How do we do this ?

推荐答案

OK,我写了code你想要的。我希望这是对别人太有用了。

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 = 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();
        }

这篇关于如何在Android的创造JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆