修改/ A jsonArray内更新值? [英] Modifying/Updating values inside a jsonArray?

查看:3326
本文介绍了修改/ A jsonArray内更新值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在特定的索引位置变化/替换一个JSON array.After在的 http://www.json.org/javadoc/org/json/JSONArray.html 我发现jsonArray没有getIndex()method.In此情况我怎么在一个给定的索引位置更新的json数组。
这是创建在我的Andr​​oid code JSON数组的方法。

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. This is the method that creates a json array in my android code.

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

这是code,我用它来更新我的JSON数组,它包含一个JSON对象。

And this is the code that I use to update my json array which contains a json object.

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

现在与此code中的问题是,它更新jsonArray每次一个新的项加入到code.So第一对象值是相同的最后一个对象

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.

另外请注意,我用这code里面的文本watcher.So的afterTextchanged()方法看起来是这样的。

Also note that I am using this code inside a text watcher.So the afterTextchanged() method looks like this.

  @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.

在这里输入的形象描述

推荐答案

据我了解,你的问题是在你的 JSONArray创建多个一个JSONObjects 具有相同的价值观,那是因为你不能得到一个创建的JSONObject 首页 C>里面的 JSONArray ,并克服这个问题,你可以轻松地创建的JSONObject 包含复合的一个JSONObjects 代替 JSONArray 包含一个JSONObjects ,和对象名称会是什么,在你的的JSONObject 数据独一无二的,当你进行任何更改或添加任何项目它要么被添加为一个新的对象,如果它不存在,或将改写现有的对象,如果它之前添加,例如让我们假设酒吧code 值在你的数据独一无二的,所以code将是这样的:

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:

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

和检索数据您通过这个简单的迭代的JSONObject

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

这篇关于修改/ A jsonArray内更新值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆