9

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

3
  • You could use replace with a regex pattern. Commented Aug 12, 2016 at 8:30
  • can u direct me to the example @Olian? Commented Aug 12, 2016 at 8:34
  • just posted an answer. Take a look :) Commented Aug 12, 2016 at 8:46

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer. However, This approach will modify the value of STATUSUPDATE in itemArr right? the values in the original JSONArray array will the remain the same as before, right? @KrutiPatel
8

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

Comments

0

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

Comments

0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.