C# 使用JSONPath编辑JSON
你好,社区:) 我想创建一个方法,使用户可以编辑(或添加)JSON特定的值或对象(位于JSON by JSONPath中)。下面是我想到的简单例子。用户总是输入JSON、JSONPath和要更改的值/对象。我正在使用Json.NET库 方法输入{json,jsonpath,valuetoedit}| |输出{new json as string} 输入示例:C# 使用JSONPath编辑JSON,c#,json,jsonpath,json.net,C#,Json,Jsonpath,Json.net,你好,社区:) 我想创建一个方法,使用户可以编辑(或添加)JSON特定的值或对象(位于JSON by JSONPath中)。下面是我想到的简单例子。用户总是输入JSON、JSONPath和要更改的值/对象。我正在使用Json.NET库 方法输入{json,jsonpath,valuetoedit}| |输出{new json as string} 输入示例: { "store": { "book": [ { "category": "reference",
{ "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
要更改的示例值:
$.store.book[*].author
NewAuthorSpecifiedByUser
输出-新的JSON,其中所有作者都将更改为“NewAuthorSpecifiedByUser”
这是可能的吗?第三方软件包可以轻松做到这一点:
JToken
JToken
层次结构中可能会比重复地从和转换为字符串表示更有效
另请参见。首先采用
code
方法,将JSON反序列化为对象图,修改对象图,然后序列化回JSON,不是更容易吗?特别是对于更大、更复杂的JSON,我认为更新为POCO
比尝试通过路径更新JSON更快。我的意思是,你可以使用所有的C#而不是一些使用可怜的javascript的开发人员(开玩笑)!工作起来很有魅力。谢谢!