如何读取和迭代JSON文件内容? [英] How to read and iterate JSON file contents?
问题描述
在下面找到我的JSON文件。
{
webroot:wwwroot,
userSecretsId:aspnet5-WebApplication7-0de8c0d7-92a5-43b4-a3f1-afeed2785dfe,
version:1.0.0- *,
dependencies:{
EntityFramework.SqlServer:7.0.0-beta5,
EntityFramework.Commands:7.0。 0-beta5,
Microsoft.AspNet.Mvc:6.0.0-beta5,
},
排除:[
wwwroot,
node_modules,
bower_components
}
我可以将webroot的值读作字符串,并使用以下代码段排除为数组。
Find my JSON file below.
{
"webroot": "wwwroot",
"userSecretsId": "aspnet5-WebApplication7-0de8c0d7-92a5-43b4-a3f1-afeed2785dfe",
"version": "1.0.0-*",
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-beta5",
"EntityFramework.Commands": "7.0.0-beta5",
"Microsoft.AspNet.Mvc": "6.0.0-beta5",
},
"exclude": [
"wwwroot",
"node_modules",
"bower_components"
}
I can read value of "webroot" as string and "exclude" as array using following snippet.
string file = File.ReadAllText("project.json");
Product pro = JsonConvert.DeserializeObject<Product>(file);
我的要求是读取依赖节点下的每个值并验证它。然后添加一个新值并将其写回json fie。请帮助。
My requirement is to read each value under dependency node and validate it. Then to add a new value and write it back to the json fie. Please help.
推荐答案
您可能需要 NewtonSoft.Json / [ ^ ]。
使用 jObject [ ^ ]
一个很好的例子:
http://www.newtonsoft.com/json/help/html/ReadJson.htm [ ^ ]和 http://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm [ ^ ]
-KR
You might need NewtonSoft.Json/[^].
Use jObject[^]
An excellent example:
http://www.newtonsoft.com/json/help/html/ReadJson.htm[^] and http://www.newtonsoft.com/json/help/html/DeserializeWithJsonSerializerFromFile.htm[^]
-KR
这篇关于如何读取和迭代JSON文件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!