【问题标题】:How to get specific information from a complex JSON format file C#如何从复杂的 JSON 格式文件 C# 中获取特定信息
【发布时间】:2020-02-19 11:21:59
【问题描述】:

我试图从一个复杂的 JSON 文件中获取所有“方法名”“全名”和属性 [] 值的列表,我试图实现一个基本模型来读取顶层,但所有内容都返回为 null;

public class TestRun
{
    public string @id { get; set; }
    public string @name { get; set; }
    public string @fullname { get; set; }
    public string testcasecount { get; set; }
    public TestSuite[] TestSuite { get; set; }
}

public class TestSuite
{

}

public class ScriptModel
{
    public TestRun[] TestRun { get; set; }

}

JSON 看起来像这样;从根级别有大量嵌套的“test-suites”和“test-fixtures”

            "test-suite": {
                "@type": "TestSuite",
                "@id": "0-1030",
                "@name": "Example__Exampledll",
                "@fullname": "Example",
                "@runstate": "Runnable",
                "@testcasecount": "24",
                "test-suite": {
                    "@type": "TestSuite",
                    "@id": "0-1031",
                    "@name": "Features",
                    "@fullname": "Payments_Regression.Features",
                    "@runstate": "Runnable",
                    "@testcasecount": "24",
                    "test-suite": [
                        {
                            "@type": "TestSuite",
                            "@id": "0-1033",
                            "@name": "PRE",
                            "@fullname": "Payments_Regression.Features",
                            "@runstate": "Runnable",
                            "@testcasecount": "16",
                            "test-suite": [
                                {
                                    "@type": "TestFixture",
                                    "@id": "0-1015",
                                    "@name": "Outwards",
                                    "@fullname": "Example_Outwards",
                                    "@classname": "Example_Dll_Example_Outwards",
                                    "@runstate": "Runnable",
                                    "@testcasecount": "8",
                                    "properties": {
                                        "property": [
                                            {
                                                "@name": "Description",
                                                "@value": "Smoke"
                                            },
                                            {
                                                "@name": "Category",
                                                "@value": "PREPRODUCTION"
                                            },
                                            {
                                                "@name": "Category",
                                                "@value": "Payment"
                                            }
                                        ]
                                    },
                                    "test-case": [
                                        {
                                            "@id": "0-1017",
                                            "@name": "TestCaseNameIWantToCapture",
                                            "@fullname": "fullnameexample",
                                            "@methodname": "MethodNameToCapture",
                                            "@classname": "ClassNametoCapture",
                                            "@runstate": "Runnable",
                                            "@seed": "1767458888",
                                            "properties": {
                                                "property": {
                                                    "@name": "Description",
                                                    "@value": "PropertyValueiWouldLikeToCapture."
                                                }
                                            }
                                        },

【问题讨论】:

  • 将您的 JSON 复制到剪贴板,然后在 Visual Studio 中,选择粘贴/特殊/粘贴 JSON 作为类。这将为您的 json 编写正确的类。
  • 您的Json 格式似乎不正确。发布有效的Json
  • 使用 JsontoCsharp 网站从这里检查您的课程 - json2csharp.com 我试图解析您的 JSON 字符串位,它似乎在 JSON 字符串中丢失了一些东西。

标签: c# json object model


【解决方案1】:

你使用特殊符号@它不会改变变量名,这个符号有时用于使用关键字,如变量名var @object = "sample"

你需要使用JsonProperty属性

public class TestRun
{
    [JsonProperty("@id")]
    public string Id { get; set; }

    [JsonProperty("@name")]
    public string Name { get; set; }

    [JsonProperty("@fullname")]
    public string FullName { get; set; }

    [JsonProperty("@testcasecount")]
    public string TestCaseCount { get; set; }

    [JsonProperty("test-suite")]
    public TestSuite[] TestSuite { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2021-12-04
    相关资源
    最近更新 更多