3

我正在尝试将一段 JSON 反序列化为 C# 对象,但到目前为止收效甚微。根据http://jsonlint.com/,以下字符串是有效的 JSON。但是,其中似乎没有任何变量名称。我习惯于看到带有“objectID”、“objectName”等字符串的 JSON。在这种情况下,我可以创建一个带有 objectID 和 objectName 等字段的 C# 对象,然后像这样反序列化整个内容: Quote deserializedProduct = JsonConvert.DeserializeObject<Product>(json);

关于我如何做到这一点的任何建议?例如,我看不到它只会反序列化为一系列锯齿状或嵌套数组的方法。将不胜感激任何帮助。

{
"ABCXYZ": {
    "Wed, Aug 29|Wed|29 Aug 12": [
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 812~ ~~ABC~08/29/2012 06:35~XYZ~08/29/2012 07:45~",
            "No Taxes",
            "2012-08-29 06:35",
            "2012-08-29 07:45",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 816~ ~~ABC~08/29/2012 14:55~XYZ~08/29/2012 16:05~",
            "No Taxes",
            "2012-08-29 14:55",
            "2012-08-29 16:05",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 818~ ~~ABC~08/29/2012 19:50~XYZ~08/29/2012 20:55~",
            "No Taxes",
            "2012-08-29 19:50",
            "2012-08-29 20:55",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ]
    ],
    "Thu, Aug 30|Thu|30 Aug 12": [
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 812~ ~~ABC~08/30/2012 06:35~XYZ~08/30/2012 07:45~",
            "No Taxes",
            "2012-08-30 06:35",
            "2012-08-30 07:45",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 816~ ~~ABC~08/30/2012 08:30~XYZ~08/30/2012 09:40~",
            "No Taxes",
            "2012-08-30 08:30",
            "2012-08-30 09:40",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 818~ ~~ABC~08/30/2012 22:10~XYZ~08/30/2012 23:15~",
            "No Taxes",
            "2012-08-30 22:10",
            "2012-08-30 23:15",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ]
    ],
    "Fri, Aug 31|Fri|31 Aug 12": [
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 812~ ~~ABC~08/31/2012 06:35~XYZ~08/31/2012 07:45~",
            "No Taxes",
            "2012-08-31 06:35",
            "2012-08-31 07:45",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~C~C4LOW~BND4~~1~X",
            "FR~ 816~ ~~ABC~08/31/2012 13:20~XYZ~08/31/2012 14:30~",
            "No Taxes",
            "2012-08-31 13:20",
            "2012-08-31 14:30",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 32.74
                        }
                    ]
                }
            ],
            2,
            0,
            0,
            0
        ],
        [
            "0~C~C4LOW~BND4~~1~X",
            "FR~ 818~ ~~ABC~08/31/2012 19:50~XYZ~08/31/2012 20:55~",
            "No Taxes",
            "2012-08-31 19:50",
            "2012-08-31 20:55",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 32.74
                        }
                    ]
                }
            ],
            3,
            0,
            0,
            0
        ]
    ],
    "Sat, Sep 01|Sat|01 Sep 12": [
        [
            "0~C~C4LOW~BND4~~1~X",
            "FR~ 812~ ~~ABC~09/01/2012 08:05~XYZ~09/01/2012 09:15~",
            "No Taxes",
            "2012-09-01 08:05",
            "2012-09-01 09:15",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 32.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~W~W4LOW~BND4~~1~X",
            "FR~ 818~ ~~ABC~09/01/2012 17:00~XYZ~09/01/2012 18:05~",
            "No Taxes",
            "2012-09-01 17:00",
            "2012-09-01 18:05",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 11.74
                        }
                    ]
                }
            ],
            2,
            0,
            0,
            0
        ]
    ],
    "Sun, Sep 02|Sun|02 Sep 12": [
        [
            "0~N~N16SEP~FCCG~~3~X",
            "FR~ 812~ ~~ABC~09/02/2012 06:35~XYZ~09/02/2012 07:45~",
            "No Taxes",
            "2012-09-02 06:35",
            "2012-09-02 07:45",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 9.75
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~H~H4LOW~BND4~~1~X",
            "FR~ 816~ ~~ABC~09/02/2012 14:55~XYZ~09/02/2012 16:05~",
            "No Taxes",
            "2012-09-02 14:55",
            "2012-09-02 16:05",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 23.74
                        }
                    ]
                }
            ],
            1,
            0,
            0,
            0
        ],
        [
            "0~K~K4LOW~BND4~~1~X",
            "FR~ 818~ ~~ABC~09/02/2012 20:25~XYZ~09/02/2012 21:30~",
            "Regular Fare",
            "2012-09-02 20:25",
            "2012-09-02 21:30",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "Tax": 36,
                            "FarePrice": 33.99
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ]
    ],
    "Mon, Sep 03|Mon|03 Sep 12": [
        [
            "0~N~N16SEP~FCCG~~3~X",
            "FR~ 812~ ~~ABC~09/03/2012 06:35~XYZ~09/03/2012 07:45~",
            "No Taxes",
            "2012-09-03 06:35",
            "2012-09-03 07:45",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 9.75
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~A~A4LOW~BND4~~1~X",
            "FR~ 818~ ~~ABC~09/03/2012 14:35~XYZ~09/03/2012 15:40~",
            "No Taxes",
            "2012-09-03 14:35",
            "2012-09-03 15:40",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "CANX": 2,
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 17.74
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~N~N16SEP~FCCG~~3~X",
            "FR~ 816~ ~~ABC~09/03/2012 22:55~XYZ~09/03/2012 23:59~",
            "No Taxes",
            "2012-09-03 22:55",
            "2012-09-03 23:59",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 9.75
                        }
                    ]
                }
            ],
            3,
            0,
            0,
            0
        ]
    ],
    "Tue, Sep 04|Tue|04 Sep 12": [
        [
            "0~N~N16SEP~FCCG~~3~X",
            "FR~ 812~ ~~ABC~09/04/2012 06:35~XYZ~09/04/2012 07:45~",
            "No Taxes",
            "2012-09-04 06:35",
            "2012-09-04 07:45",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 9.75
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ],
        [
            "0~N~N16SEP~FCCG~~3~X",
            "FR~ 818~ ~~ABC~09/04/2012 19:50~XYZ~09/04/2012 20:55~",
            "No Taxes",
            "2012-09-04 19:50",
            "2012-09-04 20:55",
            [
                {
                    "ADT": [
                        1,
                        "0.00",
                        "0",
                        {
                            "WC": 6,
                            "ETS": 0.25,
                            "FarePrice": 9.75
                        }
                    ]
                }
            ],
            -1,
            0,
            0,
            0
        ]
    ]
}

}

4

2 回答 2

3
var obj = (JObject)JsonConvert.DeserializeObject(json);

foreach (JProperty item in obj["ABCXYZ"].Children())
{
    Console.WriteLine(item.Name);
    foreach (var x in item)
    {
        foreach (var y in x)
        {
            Console.WriteLine("\t==> " + y[0]);
        }
    }
}

PS:您的 json 很难阅读。Json Viewer可以帮助轻松查看其结构。

于 2012-08-15T15:41:51.873 回答
0

Newtonsoft 是我最喜欢在 C# 中处理 JSON 的方式,我经常使用不同编程语言的 JSON,使用它就像使用 Javascript 或 PHP 一样简单。

这是最简单的例子之一

string json_string = @"{
                  Firstname: ""Jane"",
                  Lastname: ""Doe"",
                  Age: 36,
                  IsEmployed: true,
                  IsMarried: true,
                  Children: 4
              }";
              
var person = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json_string);

Console.WriteLine(person.Forename);
Console.WriteLine(person.Lastname);
Console.WriteLine(person.Age);
Console.WriteLine(person.IsEmployed);
Console.WriteLine(person.IsMarried);
Console.WriteLine(person.Children);

无论结构如何,它都会动态生成对象!其他解决方案不适用于所有 .NET 版本。

我在这里写了一篇简单易懂的文章https://turmanauli.medium.com/a-complete-guide-for-serializing-json-to-dynamic-objects-on-the-fly-in-c- 7ab4799f648d 关于如何在 Visual Studio 项目中通过 NuGet 包管理器安装和使用 Newtonsoft。

于 2021-03-20T00:08:43.093 回答