【问题标题】:c# data pull from json - can not detect the "-" sign between wordsc# data pull from json - 无法检测到单词之间的“-”符号
【发布时间】:2019-01-14 09:05:57
【问题描述】:
string json_index = '"libraries": [
    {
        "name": "test1",
        "natives": {
            "windows": "natives-windows"
        },
        "downloads": {
            "classifiers": {
                "natives-windows": {
                    "url": "http://test1.com/"
                }
            }
        }
    },
    {
        "name": "test2",
        "natives": {
            "windows": "natives-windows"
        },
        "downloads": {
            "classifiers": {
                "natives-windows": {
                    "url": "http://test2.com/"
                }
            }
        }
    }
]';
dynamic jsonObj = JsonConvert.DeserializeObject(json_index);
foreach (var obj in jsonObj.libraries)
{
    label1.Text += "\n" + obj.downloads.classifiers.natives-windows.url; // error here
}

无法检测单词之间的“-”号。

其实我是这么想的:

string nativeswindows = obj.natives.windows;
label1.Text += "\n" + obj.downloads.classifiers.nativeswindows.url;

但是没有用

如何在“natives-windows”中获取“url”?

我正在使用 Newtonsoft JSON。

【问题讨论】:

标签: c# json visual-studio json.net


【解决方案1】:

你试试:

  label1.Text += "\n" + obj.downloads.classifiers["natives-windows"].url;

我找到了这个链接:Parsing JSON w/ @ at sign symbol in it (arobase)

希望对你有帮助!

【讨论】:

    【解决方案2】:

    所以有几个步骤。

    首先,您需要定义一个具体的类来表示您的 JSON。我使用http://json2csharp.com 完成了这项工作,输出在这里:

    public class Natives
    {
        public string windows { get; set; }
    }
    
    public class NativesWindows
    {
        public string url { get; set; }
    }
    
    public class Classifiers
    {
        public NativesWindows __invalid_name__natives-windows { get; set; }
    }
    
    public class Downloads
    {
        public Classifiers classifiers { get; set; }
    }
    
    public class Library
    {
        public string name { get; set; }
        public Natives natives { get; set; }
        public Downloads downloads { get; set; }
    }
    
    public class RootObject
    {
        public List<Library> libraries { get; set; }
    }
    

    您的问题字段也已被此工具标记,见此处:

    public NativesWindows __invalid_name__natives-windows { get; set; }

    所以我们需要一种方法将 JSON 键/值对分配给有效的 C# 字段。我们可以使用Attributes 来做到这一点。

    特别是对于这个字段,我们可以使用JsonProperty 属性来获取 JSON 属性名称并将其分配给新具体类上的 C# 字段。这看起来像:

    [JsonProperty("native-windows")]
    public NativesWindows NativeWindowsObj { get; set; }
    

    您可以将其放入新的具体类中,然后使用以下内容反序列化为该类型:

    Natives jsonObj = JsonConvert.DeserializeObject&lt;Natives&gt;(json_index);

    这是在告诉 Newtonsoft:

    1. 我有一个属性名称native-windows
    2. 我正在将我的 JSON 反序列化为这个特定的类。
    3. 识别出的无效 C# native-windows 与我在课堂上指定的 JsonProperty 匹配,将值分配给该匹配属性。
    4. 返回完整的反序列化对象。

    【讨论】:

    • 以上答案是针对简单的解决方案。这个方法刚刚奏效!谢谢!
    猜你喜欢
    • 2022-01-05
    • 2012-10-21
    • 2014-03-17
    • 1970-01-01
    • 2015-08-13
    • 2013-04-06
    • 2011-11-30
    • 2011-11-02
    • 2019-01-07
    相关资源
    最近更新 更多