user3502930
user3502930

Reputation: 173

How to Update JSONArray value on java

can anyone help me, i'am new on java programing

let say i have JSONArray with this data below :

[{
    "STATUSUPDATE": 0,
    "IDSERV": "2"
}, {
   "STATUSUPDATE": 0,
   "IDSERV": "3"
}, {
   "STATUSUPDATE": 0,
   "IDSERV": "1"
}]

How to update STATUSUPDATE to 1 in IDSERV 2

How to update STATUSUPDATE to 2 in IDSERV 3

and was trying to loop the data

for (int i=0; i < array.length; i++){
JSONObject itemArr = (JSONObject)array.get(j);
if(itemArr.get("IDSERV").equals(2)){
//should be itemArr.set(with new val) 
//but method *set* can cal; only on JSONArray not an JSONObject
//and looping the next one 
}
}

can anyone help me

Upvotes: 9

Views: 40704

Answers (4)

Santhosh Hirekerur
Santhosh Hirekerur

Reputation: 876

I used replacing some fields its decimal values to zero on user role. Its working

shimpment=shimpment.replaceAll("\"shipmentValue\":[0-9]+.[0-9]+", "\"shipmentValue\":0.00");
shimpment=shimpment.replaceAll("\"price\":[0-9]+.[0-9]+", "\"price\":0.00");
shimpment=shimpment.replaceAll("\"tax\":[0-9]+.[0-9]+", "\"tax\":0.00");

Upvotes: 0

Olian04
Olian04

Reputation: 6882

Using regex and replaceAll:

String json = ...
json.replaceAll("(?<=\"IDSERV\":\")\\d*(?=\")", new value);

The above will locate and replace ALL IDSERV fields.
If you only want to find and replace ONE of the IDSERV fields, change the \\d to [] and put the expected value to swap in between the braces.
Ex: [1] will find and replace all values equal to 1.

EDIT1:
Ok, you just edited the question.

This regex allows you to target a specific IDSERV and changes its STATUSUPDATE field.

(?<=:)\d*(?=,"IDSERV":"1")

In the above, change the number 1 to whatever value of IDSERV you want to look for.

In java that would be:

String json = ...
json.replaceAll("(?<=:)\\d*(?=,\"IDSERV\":\"1\")", new value);

Upvotes: 0

Kruti Patel
Kruti Patel

Reputation: 1472

Here is the code:

array is your JSONArray

for (int i=0; i < array.length(); i++){
    JSONObject itemArr = (JSONObject)arr.get(i);
    if(itemArr.get("IDSERV").getAsString().equals("2")){
        itemArr.put("STATUSUPDATE", 1);
    }else if(itemArr.get("IDSERV").getAsString().equals("3")){
        itemArr.put("STATUSUPDATE", 2);
    }
}

Now, if you print array then you can see values are changed.

Upvotes: 8

Raman Sahasi
Raman Sahasi

Reputation: 31901

JSONArray specific code:

Output

Initial array : [{"STATUSUPDATE":0,"IDSERV":"2"},{"STATUSUPDATE":0,"IDSERV":"3"},{"STATUSUPDATE":0,"IDSERV":"1"}]
Output array : [{"STATUSUPDATE":"1","IDSERV":"2"},{"STATUSUPDATE":"2","IDSERV":"3"},{"STATUSUPDATE":0,"IDSERV":"1"}]

Code

public class Test {
    public static void main(String[] args) throws JSONException {
        JSONArray array = new JSONArray("[{\"STATUSUPDATE\":0,\"IDSERV\":\"2\"},{\"STATUSUPDATE\":0,\"IDSERV\":\"3\"},{\"STATUSUPDATE\":0,\"IDSERV\":\"1\"}]");
        System.out.println("Initial array : " + array);

        for (int i=0; i < array.length(); i++){
            JSONObject jsonObject = new JSONObject(array.get(i).toString());
            if(jsonObject.get("IDSERV").equals("2")) {
                jsonObject.put("STATUSUPDATE", "1");
                array.put(i, jsonObject);
            }
            else if(jsonObject.get("IDSERV").equals("3")) {
                jsonObject.put("STATUSUPDATE", "2");
                array.put(i, jsonObject);
            }
        }

        System.out.println("Output array : " + array);
    }
}

Upvotes: 8

Related Questions