【问题标题】:How to add data into a json array instead of a ArrayList?如何将数据添加到 json 数组而不是 ArrayList?
【发布时间】:2016-03-01 13:56:55
【问题描述】:

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

【问题讨论】:

  • 你想在这里做什么?你为什么要这样做?
  • 你遇到了什么问题
  • @AndroidNewBee 我希望它是一个 jsonArray 以便我可以解析它并使用 volley 将此数据插入 mysql 数据库。请帮助我是编程新手:) 谢谢
  • 你要做什么json??你能举个例子吗
  • @Clairvoyant 我想将地图直接插入到 jsonarray 中。但是由于我不知道该怎么做,所以我首先将地图数据放入 arraylist,然后将其转换为 json 数组。有没有一种方法可以让我使用 json 数组做同样的事情。谢谢

标签: java android arrays json


【解决方案1】:

试试这个:

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

这样,您不必经过ArrayList 来填写您的JSONArray。然后,您只需在 JSON 数组上调用 toString() 方法即可获取您的 JSON 字符串

String jsonString = itemSelectedJson.toString();

【讨论】:

  • 能否详细说明我想试试这个代码。谢谢
  • 是的,我会为此使用 volley 库。
  • 问题是,我以前从未使用过 Volley 甚至 Retrofit,所以你需要确切地告诉我你必须做什么才能向 Volley 发送成功的请求。
  • 我是编程新手,所以我想关注这个博客。 simplifiedcoding.net/android-volley-post-request-tutorial
  • 我看到已经定义了一个getParams() 方法,用于将参数附加到您的Volley 请求,并在其中返回一个Map。我认为你也应该这样做。
【解决方案2】:

你可以这样做:

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

希望这会对你有所帮助。

【讨论】:

  • 你能详细说明一下吗?我想试试这个。
  • @user3295583,您必须将您的键和值放入您的 json 对象中,并在填充数据后将 json 对象放入 json 数组中。
  • @HirenPatel 请检查下面的代码如何在这种情况下使用您的代码。即使我正在尝试做一些非常相似的事情。谢谢
猜你喜欢
  • 2011-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多