[
"label": {
"originalName" : "Case #",
"modifiedLabel" : "Case #",
"labelId" : "case_number_lbl",
"isEditable" : "true",
"imageClass" : ""
}
]
在上面的 Json 数组中,我需要将“Case #”替换为“Ticket #”。这种情况在很多地方都在发生。有的话请更新一下。 提前致谢。
最佳答案
我认为一个简单的循环应该可以解决您的问题:
public static void main(String[] args) throws JSONException {
JSONArray array = new JSONArray("[" +
" {" +
" originalName : \"Case #\"," +
" modifiedLabel : \"Case #\"," +
" labelId : \"case_number_lbl\"," +
" isEditable : \"true\"," +
" imageClass : \"\"" +
" }" +
"]");
System.out.println(array.toString(2));
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
JSONArray keys = object.names();
for (int j = 0; j < keys.length(); j++) {
String key = keys.getString(j);
if (object.getString(key).equals("Case #")) {
object.put(key, "Ticket #");
}
}
}
System.out.println();
System.out.println(array.toString(2));
}
关于java - 如何使用java替换Json中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754193/