使用 C# 和 JSON.net 读取 JSON 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19596135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:23:26  来源:igfitidea点击:

Reading JSON files with C# and JSON.net

c#jsonjson.net

提问by Daniel J?rgensen

Im having some trouble to understand how to use JSON.net to read a json file.

我在理解如何使用 JSON.net 读取 json 文件时遇到了一些麻烦。

The file is looking like this:

该文件如下所示:

"version": {   
    "files": [
        {
            "url": "http://www.url.com/",
            "name": "someName"
        },
        { 
            "name": "someOtherName"
            "url": "http://www.url.com/"
            "clientreq": true
        }, ....

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..

我真的不知道如何读取这个文件..我需要做的是读取行并通过“url”下载文件..我知道如何下载文件等等,但我不知道我如何使用 JSON.net 读取 json 文件并遍历每个部分,然后下载文件..

Can you assist ?

你能帮忙吗?

采纳答案by Esteban Elverdin

The easiest way is to deserialize your json into a dynamic object like this

最简单的方法是将您的 json 反序列化为这样的动态对象

Then you can access its properties an loop for getting the urls

然后你可以访问它的属性一个用于获取 url 的循环

dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString);

var urls = new List<string>();

foreach(var file in result.version.files)
{
    urls.Add(file.url); 
}

回答by Tim S.

http://json2csharp.com/helps you create C# classes based on your JSON data type. Once you have your classes to match your data, you can deserialize with Json.NETand then work with your data:

http://json2csharp.com/可帮助您根据 JSON 数据类型创建 C# 类。一旦您的类与您的数据相匹配,您就可以使用Json.NET进行反序列化,然后处理您的数据:

var myMessage = JsonConvert.DeserializeObject<MyMessage>(myString);
foreach (var file in myMessage.Version.Files)
{
    // download file.Url
}

Or you can access it as a dynamicobject:

或者您可以将其作为dynamic对象访问:

dynamic myMessage = JsonConvert.DeserializeObject(myString);
foreach (var file in myMessage.version.files)
{
    // download file.url
}

If you use classes, they might be:

如果您使用类,它们可能是:

public class File
{
    public Uri Url { get; set; }
    public string Name { get; set; }
    public bool? ClientReq { get; set; }
}

public class Version
{
    public IList<File> Files { get; set; }
}

public class MyMessage
{
    public Version Version { get; set; }
}

(note that Json.Net is smart enough to map properties where the case is different, and turn the URLs into Uri objects) It works when the string is like:

(请注意,Json.Net 足够智能,可以映射大小写不同的属性,并将 URL 转换为 Uri 对象)它在字符串如下时起作用:

string myString = @"{""version"": {   
    ""files"": [
        {
            ""url"": ""http://www.url.com/"",
            ""name"": ""someName""
        },
        { 
            ""name"": ""someOtherName"",
            ""url"": ""http://www.url.com/"",
            ""clientreq"": true
        }]}}";