简体   繁体   English

如何将数据添加到json数组而不是ArrayList中?

[英]How to add data into a json array instead of a ArrayList?

What I am trying to do is add a Map into an ArrayList and then into a JsonArray . 我想做的是将Map添加到ArrayList ,然后添加到JsonArray What I intend to do is directly add the maps into the json array. 我打算做的就是直接将地图添加到json数组中。

//Initialize the ArrayList,Map and Json array
private ArrayList<Map<String, String>> itemData = new ArrayList<>();
private Map<String, String> itemselected = new HashMap<>();
JSONArray itemSelectedJson = new JSONArray();



 private void selectedItems() {
    if(invEstSwitch.isChecked())
    {
        billType = textViewEstimate.getText().toString();
    }else{
        billType = textViewInvoice.getText().toString();
    }

    itemselected.put("custInfo",custSelected.toString());
    itemselected.put("invoiceNo", textViewInvNo.getText().toString());
    itemselected.put("barcode", barCode.getText().toString());
    itemselected.put("desc", itemDesc.getText().toString());
    itemselected.put("weight", weightLine.getText().toString());
    itemselected.put("rate", rateAmount.getText().toString());
    itemselected.put("makingAmt", makingAmount.getText().toString());
    itemselected.put("net_rate", netRate.getText().toString());
    itemselected.put("itemTotal", itemtotal.getText().toString());
    itemselected.put("vat", textViewVat.getText().toString());
    itemselected.put("sum_total", textViewSum.getText().toString());
    itemselected.put("bill_type", billType);
    itemselected.put("date", textViewCurrentDate.getText().toString());

    //Add the map to the Array
    itemData.add(index, itemselected);
    itemSelectedJson= new JSONArray(Arrays.asList(itemData));
    index++;
}

You can do this way: 您可以这样做:

JSONArray jRootArray = new JSONArray();

        for (int i = 1; i <= 20; i++) {
            JSONObject jInnerObject = new JSONObject();
            try {
                jInnerObject.put(String.valueOf(i), "Hello "+i);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            jRootArray.put(jInnerObject);
        }

        Log.i("JRootArray", jRootArray.toString());

Hope this will help you. 希望这会帮助你。

Try this: 尝试这个:

private Map<String, String> itemselected = new HashMap<>();   

private void selectedItems() {

    JSONArray itemSelectedJson = new JSONArray();

    // Retrieve all keys
    Set<String> keys = itemselected.keySet(); 

    // Add items to JSONArray as JSONObjects
    for(String key : keys) {
        itemSelectedJson.put(
            new JSONObject().put(key, itemselected.get(key))
        );
    }
}

This way, you won't have to pass by an ArrayList to fill your JSONArray . 这样,您将不必通过ArrayList来填充JSONArray Then, you can get your JSON string simply by calling the toString() method on the JSON array: 然后,您只需在JSON数组上调用toString()方法即可获取JSON字符串

String jsonString = itemSelectedJson.toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM