如何在 Java 上更新 JSONArray 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38913318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Update JSONArray value on java
提问by user3502930
can anyone help me, i'am new on java programing
任何人都可以帮助我,我是 Java 编程新手
let say i have JSONArray with this data below :
假设我有以下数据的 JSONArray:
[{
"STATUSUPDATE": 0,
"IDSERV": "2"
}, {
"STATUSUPDATE": 0,
"IDSERV": "3"
}, {
"STATUSUPDATE": 0,
"IDSERV": "1"
}]
How to update STATUSUPDATE
to 1
in IDSERV
2
如何更新STATUSUPDATE
到1
中IDSERV
2
How to update STATUSUPDATE
to 2
in IDSERV
3
如何更新STATUSUPDATE
到2
中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
谁能帮我
采纳答案by Kruti Patel
Here is the code:
这是代码:
array
is your JSONArray
array
是你的 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.
现在,如果您打印,array
则可以看到值已更改。
回答by Raman Sahasi
JSONArray
specific code:
JSONArray
具体代码:
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);
}
}
回答by Santhosh Hirekerur
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");
回答by Olian04
Using regexand replaceAll:
使用正则表达式和replaceAll:
String json = ...
json.replaceAll("(?<=\"IDSERV\":\")\d*(?=\")", new value);
The above will locate and replace ALL IDSERVfields.
If you only want to find and replace ONE of the IDSERVfields, 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
.
以上将定位并替换所有IDSERV字段。
如果您只想查找和替换IDSERV字段之一,请将更改\\d
为[]
并将预期值放在大括号之间进行交换。
例如:[1]
将查找并替换所有等于 的值1
。
EDIT1:
Ok, you just edited the question.
EDIT1:
好的,您刚刚编辑了问题。
This regex allows you to target a specific IDSERV and changes its STATUSUPDATE field.
此正则表达式允许您针对特定的 IDSERV 并更改其 STATUSUPDATE 字段。
(?<=:)\d*(?=,"IDSERV":"1")
In the above, change the number 1
to whatever value of IDSERV you want to look for.
在上面,将数字更改为1
您要查找的 IDSERV 的任何值。
In java that would be:
在java中,这将是:
String json = ...
json.replaceAll("(?<=:)\d*(?=,\"IDSERV\":\"1\")", new value);