如何在Android中创建JSON

如何在Android中创建JSON,android,json,Android,Json,如何在Android中创建嵌套JSON。JSON数据应该如下所示: JSON Customername = Roshan Customercode = 100 Customercity = Ernakulam Customerstate = Kerala Customercountry = India Customersales Productname = Biscuit Productcode = 123 Producttype = Food Customersales

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

您可以使用JSONObject

创建JSONObject:

JSONObject json = new JSONObject();
添加元素:

json.accumulate(key,value);
获取一个JSON:

json.toString;

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

    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();
        }
TextView测试文本;
testText=(TextView)findViewById(R.id.testText);
JSONObject客户、customerSales;
JSONArray客户;
列出产品清单;
//填充4个客户信息以进行测试
客户=新的JSONArray();
试一试{
对于(int i=0;i<4;i++){
//创建新客户
客户=新的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));
//为每个客户添加3个产品销售
productList=新的ArrayList();
对于(int j=0;j<3;j++){
customerSales=新的JSONObject();
customerSales.put(“产品名称”、“产品名称”+(j+1));
customerSales.put(“产品代码”,200+j+1);
customerSales.put(“产品类型”、“产品类型”+(j+1));
productList.add(customerSales);
}
customer.put(“Customersales”,productList);
客户。放置(客户);
}
}捕获(JSONException e){
e、 printStackTrace();
}
//访问客户信息
试一试{
testText.setText(“”);
对于(int i=0;i
如果您尝试过任何东西,请发布。使用Gson或Jakson:)用杰克逊来做这个!可能是重复的,谢谢你。这是可行的,但有一个小问题。对于每个内部JSON,每个双引号(“)表示如下\”。因此,当我通过fiddler将数据发送到服务器时,子数组没有被插入。怎么纠正?什么?!!什么是小提琴手??你为什么不干脆用loopj?甚至Instagram和Pinterest也在使用它。很简单,你问过如何创建嵌套的json。这就是你的答案,如果你想知道如何处理你在服务器上发送的json,你应该在其他地方搜索或请求。将customers.toString()作为参数发送到loopj的库中。然后在服务器中用php像这样获取它们:if(isset($\u POST['test']){$contents=$\u POST['test'];$contents=utf8\u encode($contents);$contents=json\u decode($contents,true);if(json\u last\u error()==json\u error\u NONE){echo“ready\n”;echo$contents[0][“Customername”];}其他{echo“未准备就绪”;}