【问题标题】:JsonConvert help reading from fileJsonConvert 帮助从文件中读取
【发布时间】:2015-12-04 09:23:01
【问题描述】:

我正在尝试从这个 json 文件中读取,问题是我没有得到任何信息,而且1357 总是在变化。

这是文件的一部分

{
    "result": {
        "1357": {
            "Random": "4NUX4oFJZEHLbXH5ApeO4",
            "Random2": "Co04DEVlxkKgpou-6kej",

我用来阅读的课程

using System;
using System.IO;
using Newtonsoft.Json;

namespace MyNamespace
{
    public class ReadRandom
    {
        public static ReadRandom Fetch(string filename)
        {
            TextReader reader = new StreamReader(filename);
            string json = reader.ReadToEnd();
            reader.Close();

            ReadRandomResult AssetClassInfoResult = JsonConvert.DeserializeObject<ReadRandomResult>(json);

            return AssetClassInfoResult.result;
        }

        [JsonProperty("Random")]
        public string Random { get; set; }

        [JsonProperty("Random2")]
        public string Random2 { get; set; }


        protected class ReadRandomResult
        {
            public ReadRandom result { get; set; }
        }

    }
}

其中一个答案的错误

Exception thrown: 'System.ArgumentException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll
Exception thrown: 'Newtonsoft.Json.JsonSerializationException' in Newtonsoft.Json.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll
An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.

http://pastebin.com/Hvp4CXXw

我正在尝试阅读“icon_url” 在那之下的一些事情。 抱歉,在我认为我不需要它之前我没有把它全部发布 在示例中使用它时,您发布了一个更改键到正确的键,我仍然崩溃。

【问题讨论】:

    标签: c# json.net


    【解决方案1】:

    问题是你在result 属性和RandomRandom2 的对象之间多了一个“层”。看起来你想要这样的东西:

    class ReadRandomResult
    {
        public Dictionary<string, ReadRandom> Result { get; set; }
    }
    

    那么你就可以使用了:

    var container = JsonConvert.DeserializeObject<ReadRandomResult>(json);
    var result = container.Result["1357"];
    

    作为说明,我强烈建议您将ReadRandomResult 类移到ReadRandom 之外...将“容器”类嵌套在它所包含的类 是很不寻常的。这可能会引起混淆。

    这是一个简短但完整的有效示例:

    using System;
    using System.Collections.Generic;
    using System.IO;
    
    using Newtonsoft.Json;
    
    class ReadRandom
    {
        [JsonProperty("Random")]
        public string Random { get; set; }
    
        [JsonProperty("Random2")]
        public string Random2 { get; set; }
    }
    
    class ReadRandomContainer
    {
        [JsonProperty("Result")]
        public Dictionary<string, ReadRandom> Result { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var json = File.ReadAllText("test.json");
            var container = JsonConvert.DeserializeObject<ReadRandomContainer>(json);
            Console.WriteLine(container.Result["1357"].Random);
        }
    }
    

    test.json的内容:

    {
        "result": {
            "1357": {
                "Random": "4NUX4oFJZEHLbXH5ApeO4",
                "Random2": "Co04DEVlxkKgpou-6kej"
            }
        }
    }
    

    输出:

    4NUX4oFJZEHLbXH5ApeO4
    

    【讨论】:

    • 当我尝试这个时,我似乎总是在 DeserializeObject 上崩溃
    • @ramafe:好吧,如果没有比“崩溃”更多的细节,就不可能为您提供任何进一步的帮助。我建议您在问题中生成一个简短但完整的示例,以及崩溃的详细信息(完整的堆栈跟踪)。
    • 我发布了我在问题中遇到的错误。仍在尝试找出原因
    • @ramafe:没有简短但完整的示例,我们无法为您提供更多帮助。我已经编辑了我的答案,以展示一个简短但完整的示例确实有效。
    • 好的,我想我已经弄清楚发生了什么,您的示例有效,我得到的结果与您所说的相同。我认为问题在于我正在阅读的更大的 json 文件。这是我返回阅读我的"{\r\n\t\"result\": {\r\n\t\t\"1357\": {\r\n\t\t\t\"Random\": 的刺痛的开始,这是我从你的示例中得到的字符串"{\r\n \"result\": {\r\n \"1357\": {\r\n \"Random\":
    【解决方案2】:

    您发布的 json 似乎无效。解析器无法读取。

    --- 编辑:

    至少你需要

    {
        "result": {
            "1357": {
                "Random": "4NUX4oFJZEHLbXH5ApeO4",
                "Random2": "Co04DEVlxkKgpou-6kej"
            }
        }
    }
    

    【讨论】:

    • @ramafe 他想说“发布完整的 JSON 文件”。
    • 它不完整是有原因的吗?例如,如果它是一个配置文件,您可以对其进行编辑。如果它来自 api,请联系 api 开发人员。因为无效的 json 可能不会受到任何人的青睐。
    • 我只发布了它的顶部,我也从互联网上下载它,我无法更改或不需要我只需要从中读取几行
    【解决方案3】:

    您忽略了您拥有1357 的事实。假设您有以下 JSON:

    {
        "result": {
            "1357": {
                "Random": "4NUX4oFJZEHLbXH5ApeO4",
                "Random2": "Co04DEVlxkKgpou-6kej"
            } 
        }
    }
    

    您的result 不是ReadRandom,而是Dictionary&lt;string, ReadRandom&gt;

    这个应该可以的:

    public class ReadRandom
    {
        public static Dictionary<string, ReadRandom> Fetch(string filename)
        {
            TextReader reader = new StreamReader(filename);
            string json = reader.ReadToEnd();
            reader.Close();
    
            return JsonConvert.DeserializeObject<ReadRandomResult>(json).result;
        }
    
        [JsonProperty("Random")]
        public string Random { get; set; }
    
        [JsonProperty("Random2")]
        public string Random2 { get; set; }
    
    
        protected class ReadRandomResult
        {
            public Dictionary<string, ReadRandom> result { get; set; }
        }
    }
    
    // Usage example:
    string random = ReadRandom.Fetch(@"C:\filename.txt")["1357"].Random;
    

    只是我的看法:在模型类中实现Fetch 并不是一个好主意。最好实现类似ReadRandomParse 的东西,它会返回ReadRandomResult

    【讨论】:

      【解决方案4】:

      检查这个链接,其中 json 字符串具有动态键,它们使用 Idictionary 然后迭代。 Deserializing JSON with dynamic keys。然后从剩余的字符串中,您可以再次反序列化为您想要的对象

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-09-07
        • 2011-03-24
        • 2011-05-23
        • 1970-01-01
        • 1970-01-01
        • 2021-02-10
        • 2011-03-28
        • 1970-01-01
        相关资源
        最近更新 更多