gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 08:39:40 24 4
gpt4 key购买 nike

我想做的是将 map 添加到 ArrayList 中,然后再添加到 JsonArray 中。我打算做的是直接将 map 添加到 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++;
}

最佳答案

你可以这样做:

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

希望对您有所帮助。

关于java - 如何将数据添加到 json 数组而不是 ArrayList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33956119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com