我是Json東西的新手,即JSON PATCH。 我有場景,我需要弄清楚兩個版本的Json文件的同一對象,因爲我使用的是json-patch-master。 但不幸的是,該補丁生成的解釋不同,即順序不同,因此得到意外/無效的結果。 任何人都可以幫助我如何保持秩序,同時生成JSON補丁?有沒有辦法在生成json文件補丁時保留順序?
**Here is the actual example.
Original Json file :**
[ {
"name" : "name1",
"roolNo" : "1"
}, {
"name" : "name2",
"roolNo" : "2"
}, {
"name" : "name3",
"roolNo" : "3"
}, {
"name" : "name4",
"roolNo" : "4"
} ]
**Modified/New Json file: i.e. removed 2nd node of original file.**
[ {
"name" : "name1",
"roolNo" : "1"
}, {
"name" : "name3",
"roolNo" : "3"
}, {
"name" : "name4",
"roolNo" : "4"
} ]
**Patch/Diff Generated :**
[ {"op":"remove","path":"/3"},
{"op":"replace","path":"/1/name","value":"name3"},
{"op":"replace","path":"/1/roolNo","value":"3"},
{"op":"replace","path":"/2/name","value":"name4"},
{"op":"replace","path":"/2/roolNo","value":"4"}]
Very time I generate Diff/Patch it is giving different path/diff results.
And moreover the interpretation is different i.e. order is not preserving.
**Is there any way to get expected results i.e. [ {"op":"remove","path":"/1"} ] , in other words generated a patch/diff based some order so will get what is expected. ?
How to handle this kind of scenario ?**
Please help me.
Thank you so much.
~Shyam
有趣的問題!我幫助維護的一個庫(https://github.com/Starcounter-Jack/JSON-Patch)也存在這個問題:http://jsfiddle.net/warpech/1epzuoj3/。我認爲它可以固定支持'Array.observe'的瀏覽器。但對於舊版瀏覽器,需要進行昂貴的髒檢查。 – warpech 2015-04-03 08:42:34