简体   繁体   English

修改/更新jsonArray中的值?

[英]Modifying/Updating values inside a jsonArray?

What I want to do is at a particular index position change/replace a value inside a json array.After going through the documentation at http://www.json.org/javadoc/org/json/JSONArray.html I found out that jsonArray does not have a getIndex() method.In this situation how do I update my json array at a given index position. 我想要做的是在特定的索引位置更改/替换json数组中的值。在浏览http://www.json.org/javadoc/org/json/JSONArray.html上的文档后,我发现了jsonArray没有getIndex()方法。在这种情况下,如何在给定的索引位置更新我的json数组。 This is the method that creates a json array in my android code. 这是在我的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();
        }
    }

And this is the code that I use to update my json array which contains a json object. 这是我用来更新包含json对象的json数组的代码。

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

Now the problem with this code is it updates the jsonArray everytime a new item is added to the code.So the first object values are the same as the last object. 现在这个代码的问题是每次将新项添加到代码时它都会更新jsonArray。因此,第一个对象值与最后一个对象相同。

Also note that I am using this code inside a text watcher.So the afterTextchanged() method looks like this. 另请注意,我在文本观察器中使用此代码。所以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();
            }
  }
    };

This is the snapshot of what my database looks like. 这是我的数据库的快照。

在此输入图像描述

A jSONObject(which is a collection of name,value pairs) can be converted into a JSONArray which is an ordered array of the "values" in the JSONObject. 可以将jSONObject(它是名称,值对的集合)转换为JSONArray,它是JSONObject中“值”的有序数组。

This can be done using the .toJSONArray() method. 这可以使用.toJSONArray()方法完成。

When you need to replace/update the JSONArray, you may use the method .put(int index, java.util.Map value) 当您需要替换/更新JSONArray时,您可以使用方法.put(int index,java.util.Map value)

Unlike how you are doing at present, ie getting the object and setting a new key and value. 与您目前的工作方式不同,即获取对象并设置新的键和值。

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

As I understood, your problem is in creating multiple JSONObjects in your JSONArray with the same values, and that's because you can't get the index of a created JSONObject inside your JSONArray , and to overcome this problem, you can easily create a compound JSONObject that contains your JSONObjects instead of a JSONArray contains JSONObjects , and the object name will be anything unique in your JSONObject data, and when you make any changes or add any item it will either be added as a new object if it doesn't exist or it will overwrite the existing object if it added before, for example let's suppose that barcode value is unique in your data, so the code will be like the following: 据我所知,您的问题是在JSONArray使用相同的值创建多个JSONObjects ,这是因为您无法在JSONArray获取已创建的JSONObjectindex ,并且为了克服此问题,您可以轻松地创建复合JSONObject包含JSONObjects而不是JSONArray JSONObjects包含JSONObjects ,对象名称将是您的JSONObject数据中唯一的任何内容,当您进行任何更改或添加任何项目时,如果它不存在,则将其添加为新对象或如果之前添加的话,它会覆盖现有的对象,例如假设条形码值在数据中是唯一的,因此代码如下所示:

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

and to retrieve the data you simple iterate through this 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
}

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

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