【发布时间】:2016-03-04 14:53:46
【问题描述】:
你好 SO 社区 :)!
我想创建允许用户编辑(或添加)JSON 特定值或对象(由 JSONPath 位于 JSON 中)的方法。下面简单的例子是我的想法。用户总是输入 JSON、JSONPath 和值/对象来更改。我正在使用 Json.NET 库。
方法输入 {json, jsonpath, valuetoedit} || 输出 {new json as string}
示例输入:
{ "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
输出 - 所有作者都将更改为“NewAuthorSpecifiedByUser”的新 JSON。
这可能吗?
【问题讨论】:
-
采用
code-first的方式,将JSON反序列化为对象图,修改对象图,然后再序列化回JSON不是更简单吗?特别是对于更大更复杂的 JSON,我认为更新为POCO比尝试通过路径更新 json 更快。我的意思是,你可以使用所有的 C#,而不是一些使用微不足道的 javascript 的开发人员(开玩笑)!