簡體   English   中英

修改/更新jsonArray中的值?

[英]Modifying/Updating values inside a jsonArray?

我想要做的是在特定的索引位置更改/替換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();
            }
  }
    };

這是我的數據庫的快照。

在此輸入圖像描述

可以將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

據我所知,您的問題是在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);

並檢索您通過此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