阅读JSON文件和C#和JSON.net
问题描述:
林遇到了一些麻烦,以了解如何使用JSON.net读取JSON文件
Im having some trouble to understand how to use JSON.net to read a json file.
该文件看起来像这样:
"version": {
"files": [
{
"url": "http://www.url.com/",
"name": "someName"
},
{
"name": "someOtherName"
"url": "http://www.url.com/"
"clientreq": true
}, ....
我真的没有太多的想法如何,我可以读取该文件..我需要做的是阅读的线条和通过URL下载文件..我知道如何下载文件等等,但我不知道我可以使用JSON.net通读每个部分的JSON文件和环路,并下载该文件。
I really do not have much idea how i can read this file .. What i need to do is to read the lines and download the file via the "url".. I know how to download files and so on, but i dont know how i can use JSON.net to read the json file and loop through each section, and download the file..
你能帮助?
答
最简单的方法就是反序列化JSON成这样
The easiest way is to deserialize your json into a dynamic object like this
然后你就可以访问它的属性的循环用于获取网址
Then you can access its properties an loop for getting the urls
dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);
var urls = new List<string>();
foreach(var file in result.version.files)
{
urls.Add(file.url);
}