你好SO社區:)!使用JSONPath編輯JSON
我想創建一個方法,讓用戶編輯(或添加)JSON特定值或對象(位於JSON中的JSONPath)。下面是一個簡單的例子,我的想法是什麼用戶總是輸入JSON,JSONPath和值/對象進行更改。我正在使用Json.NET庫。
方法輸入 {JSON,jsonpath,valuetoedit} || 輸出 {新JSON作爲字符串}
示例輸入:
{ "store": {
"book": [
{ "category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{ "category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{ "category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{ "category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
}
}
例JSONPath:
$.store.book[*].author
值示例改變:
NewAuthorSpecifiedByUser
輸出 - 新的JSON,其中所有作者將被更改爲「NewAuthorSpecifiedByUser」。
這可能嗎?
採用「先代碼先行」的方法並將JSON反序列化爲對象圖,修改對象圖,然後序列化回JSON不是更容易嗎?特別是對於更大更復雜的JSON,我認爲更新爲「POCO」會更快,而不是嘗試按路徑更新json。我的意思是,你擁有C#的所有功能,而不是一些使用麻煩的JavaScript(開玩笑)的開發者! – code4life