Java – Modify/update values in jsonArray?

Modify/update values in jsonArray?… here is a solution to the problem.

Modify/update values in jsonArray?

What I want to do is change/replace the value in the json array at a specific index position. Read http://www.json.org/javadoc/org/json/JSONArray.html After the documentation I found that jsonArray does not have a getIndex() method. How do I update my json array at the given index position in this case.
This is the way to create 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();
        }
    }

This is the code I used to update the JSON array containing the JSON object.

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

The problem with this code now is that it updates the jsonArray every time a new item is added to the code. Therefore, the value of the first object is the same as the value of the last object.

Also note that I use this code in a text viewer. Therefore, 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 a snapshot of my database.

enter image description here

Solution

jSONObject (a collection of names, value pairs) can be converted to JSONArray, which is an ordered array of “values” in JSONObject.

This can be done using the .toJSONArray() method.

You can use methods when you need to replace/update JSONArray
.put(int index, java.util.Map value)

It’s a departure from what you currently do, which is to get the object and set new keys and values.

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

Related Problems and Solutions