[英]Modify JSON-File and save it back as a file in exactly the same order with Java
我有一個要讀取的json文件,然后修改特定鍵的值,然后以完全相同的順序保存它。
我成功地完成了修改,但是在JSONArray中,現在將其編寫為JSON-File(JSONString)時,遇到兩個問題:
1)參數(鍵)的順序改變
2)德語字母被編碼為奇怪的字符。
那么,如何以與讀取時完全相同的順序保存更改,並保留原樣的德語字符來進行修改?
這是我的json文件的結構:
[{
"key1": "key here",
"key2": "key2 here",
"key3": "pId here11",
"details": {
"detailsKey1": "https://linkHere.com/conf/conf1/conf2/img/testImage.png",
"detailsKey2": "desc here",
"detailsKey3": "some url here",
"detailsKeys4": "some key here",
"terms": [{
"termsKey1": "über dreißig",
"termsKey2": "mäßig term here"
}]
}
}, {
"key1": "key here",
"key2": "key2 here",
"key3": "pId here11",
"details": {
"detailsKey1": "https://linkHere.com/conf/conf1/conf2/img/testImage.png",
"detailsKey2": "desc here",
"detailsKey3": "some url here",
"detailsKeys4": "some key here",
"terms": [{
"termsKey1": "über dreißig",
"termsKey2": "mäßig term here"
}]
}
}]
我只想修改detailsKey1的值! 我想用新的字符串更改文件中每個對象的鏈接。
這是我的代碼:
public class testi {
public static void main(String[] args) throws JSONException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String jsonOldData = readFile("C:\\Users\\myuser\\Desktop\\img.json");
JSONArray jOldArray = new JSONArray(jsonOldData);
ArrayList<String> myOldArray = new ArrayList<>();
System.out.println("length old: " + jOldArray.length());
for(int i=0; i < jOldArray.length(); i++) {
System.out.println("link here" + i + ": " + jOldArray.getJSONObject(i).getJSONObject("details").getString("detailsKey1"));
if (i == 0) {
jOldArray.getJSONObject(i).getJSONObject("details").put("detailsKey1", "testImage.png");
System.out.println("NEW teaserImage " + i + ": " + jOldArray.getJSONObject(i).getJSONObject("details").getString("detailsKey1"));
}
if (i==2)
System.out.println("teaserImage " + (i-2) + ": " + jOldArray.getJSONObject(i-2).getJSONObject("details").getString("detailsKey1"));
}
}
public static String readFile(String filename) {
String result = "";
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
result = sb.toString();
} catch(Exception e) {
e.printStackTrace();
}
return result;
}
}
如您所見/測試,更改發生在數組中,但是我不知道現在要怎么做,才能用相同的鍵將新的(更新的)文件保存在桌面中。
JSON對象是根據JSON Spec的無序名稱/值對集合; 所以我首先要問這個功能的必要性。
另外,雖然Java中的JsonArray庫是按照此規范編寫的,但您可以獲取JSON中項目的索引,但是JsonWriter不允許您指定要插入JSON的索引。
如果需要訂購,我建議您編寫自己的不符合JSON規范的解析器,那么再次需要的工作似乎超過了任何潛在的好處。
Json不會維護數據中對象的順序...它在規范中。...請通過此鏈接獲取更多信息... 鏈接
關於編碼問題,請嘗試使用稱為gson的Google json庫。...下面的代碼可以按原樣打印數據...
public static void main(String[] args) {
HashMap<String, String> a = new HashMap<String, String>();
a.put("über dreißig", "mäßig term here");
System.out.println(new Gson().toJson(a));
}
gson的maven依賴關系如下:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.