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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33956119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:20:39  来源:igfitidea点击:

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

javaandroidarraysjson

提问by

What I am trying to do is add a Map into an ArrayListand then into a JsonArray. What I intend to do is directly add the maps into the json array.

我想要做的是将 Map 添加ArrayListJsonArray. 我打算做的是直接将地图添加到 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++;
}

回答by Hiren Patel

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.

希望这会帮助你。

回答by Mohammed Aouf Zouag

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 ArrayListto fill your JSONArray. Then, you can get your JSON stringsimply by calling the toString()method on the JSON array:

这样,您就不必通过 anArrayList来填充您的JSONArray. 然后,您可以通过调用JSON 数组上的方法来获取您的JSON 字符串toString()

String jsonString = itemSelectedJson.toString();