【问题标题】:Modifying/Updating values inside a jsonArray?修改/更新 jsonArray 中的值?
【发布时间】:2016-03-13 03:26:33
【问题描述】:

我想要做的是在特定的索引位置更改/替换 json 数组中的值。在查看 http://www.json.org/javadoc/org/json/JSONArray.html 的文档后,我发现 jsonArray 没有 getIndex() 方法。在这种情况下如何在给定的索引位置更新我的 json 数组。 这是在我的 android 代码中创建 json 数组的方法。

private void createJsonArray() {
        billType = (invEstSwitch.isChecked() ? textViewEstimate : textViewInvoice)
                .getText().toString();
        String invNumber = textViewInvNo.getText().toString();
        String bcode = barCode.getText().toString();
        String description = itemDesc.getText().toString();
        String wt = weightLine.getText().toString();
        String rateAmt = rateAmount.getText().toString();
        String making = makingAmount.getText().toString();
        String netr = netRate.getText().toString();
        String iTotal = itemtotal.getText().toString();
        String vatAmt = textViewVat.getText().toString();
        String sumAmt = textViewSum.getText().toString();
        String crtDate = textViewCurrentDate.getText().toString();
        try {
            jsonObject.put("custInfo", custSelected.toString());
            jsonObject.put("invoiceNo", invNumber);
            jsonObject.put("barcode", bcode);
            jsonObject.put("description", description);
            jsonObject.put("weight", wt);
            jsonObject.put("rate", rateAmt);
            jsonObject.put("makingAmt", making);
            jsonObject.put("net_rate", netr);
            jsonObject.put("itemTotal", iTotal);
            jsonObject.put("vat", vatAmt);
            jsonObject.put("sum_total", sumAmt);
            jsonObject.put("bill_type", billType);
            jsonObject.put("date", crtDate);


        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            itemSelectedJson.put(index, jsonObject);
            index++;
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

这是我用来更新包含 json 对象的 json 数组的代码。

  try {
                itemSelectedJson.getJSONObject(i).put("net_rate",netChange);
                Log.d("NETRATE_TW",itemSelectedJson.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }

现在这段代码的问题是每次向代码中添加新项目时它都会更新 jsonArray。所以第一个对象的值与最后一个对象相同。

另外请注意,我在文本观察器中使用此代码。所以 afterTextchanged() 方法看起来像这样。

  @Override
        public void afterTextChanged(Editable s) {
            String netChange = netRate.getText().toString();
            final int row_id = (int) newRow.getTag();

            if ((row_id<0) || (row_id> itemSelectedJson.length())){
                return;
            }

            try {
                itemSelectedJson.getJSONObject(row_id-1).put("net_rate",netChange);
                Log.d("NETRATE_TW",itemSelectedJson.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
  }
    };

这是我的数据库的快照。

【问题讨论】:

  • 这里的“newRow”是什么?
  • @Hitesh542 newRow 是我动态添加的表格行
  • @DroidDev 谢谢 :) 你能帮忙解答吗?

标签: java android arrays json


【解决方案1】:

据我了解,您的问题是在您的 JSONArray 中创建多个具有相同值的 JSONObjects,这是因为您无法在您的 JSONArray 中获取创建的 JSONObjectindex,为了克服这个问题,您可以轻松地创建一个复合 JSONObject 包含您的 JSONObjects 而不是 JSONArray 包含 JSONObjects,并且对象名称将是您的 JSONObject 数据中唯一的任何东西,并且当您进行任何更改或添加任何项目,如果它不存在,它将被添加为新对象,或者如果它之前添加,它将覆盖现有对象,例如让我们假设 barcode 值是唯一的在您的数据中,因此代码将如下所示:

// declaring itemSelectedJson as JSONObject 
JSONObject itemSelectedJson = new JSONObject();
.
.
.
// when wanting to add a new item
itemSelectedJson.put(jsonObject.getString("barcode"), jsonObject);

并检索您简单地遍历此JSONObject 的数据:

Iterator<?> keys = itemSelectedJson.keys();
JSONObject single_item;
while(keys.hasNext()) {
    String key = (String)keys.next();
    single_item = itemSelectedJson.getJSONObject(key);
    // do what do you want here
}

【讨论】:

    【解决方案2】:

    一个 jSONObject(它是名称、值对的集合)可以转换为 JSONArray,它是 JSONObject 中“值”的有序数组。

    这可以使用 .toJSONArray() 方法来完成。

    当您需要替换/更新 JSONArray 时,您可以使用该方法 .put(int index, java.util.Map value)

    与您目前的做法不同,即获取对象并设置新的键和值。

    http://www.json.org/javadoc/org/json/JSONArray.html#put(int, java.util.Map)

    【讨论】:

    • 欢迎使用stackoverflow 能否举个例子或说明如何实现它。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2020-01-29
    • 1970-01-01
    • 2020-02-10
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多