我有这样的jsonArray字符串:
[
[
"Penguins.jpg",
"777835.0",
"image/jpeg",
"0"
],
[
"Lighthouse.jpg",
"561276.0",
"image/jpeg",
"0"
],
[
"Tulips.jpg",
"620888.0",
"image/jpeg",
"0"
]
]
如何替换其元素并使它再次成为字符串?我尝试过类似的方法,但不起作用。
String AttchDetails = "[[\"Penguins.jpg\",\"777835.0\",\"image/jpeg\",\"0\"],[\"Lighthouse.jpg\",\"561276.0\",\"image/jpeg\",\"0\"],[\"Tulips.jpg\",\"620888.0\",\"image/jpeg\",\"0\"]]";
JsonParser parser = new JsonParser();
JsonElement myElement = parser.parse(AttchDetails);
JsonArray element1 = myElement.getAsJsonArray();
for (int i = 0 ; i < element1.size(); i++) {
JsonArray arr = element1.get(i).getAsJsonArray();
String fileName = arr.get(0).getAsString();
String fileMime = arr.get(2).getAsString();
String newName = "some_new_name_"+i+".jpg";
arr.set(0, new JsonPrimitive(newName));
}
Gson gson = new Gson();
AttchDetails = gson.toJson(AttchDetails);
System.out.println("AttchDetails : "+AttchDetails);
我正在尝试替换循环内索引0处的文件名,并使它再次成为字符串。
forloop内的替换有效,但是在forloop之外将其转换为字符串不起作用时,它将获取原始文件名 Penguins.jpg,Lighthouse.jpg,Tulips.jpg而不是some_new_name_0.jpg,some_new_name_1.jpg,some_new_name_2.jpg
任何帮助都很好,谢谢