在C#中将有效的JSON转换为对象

我试图将一段JSON反序列化为一个C#对象,到目前为止收效甚微。 根据http://jsonlint.com/,以下字符串是有效的JSON。 但是,其中似乎没有任何变量名称。 我习惯于看到带有“objectID”,“objectName”等字符串的JSON。 在这种情况下,我可以创建一个C#对象,其中包含objectID和objectName等字段,然后反序列化所有内容,如下所示: 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
        ]
    ]
}

}


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可以帮助您轻松查看它的结构。

链接地址: http://www.djcxy.com/p/6457.html

上一篇: Valid JSON to Object in C#

下一篇: How do I turn a C# object into a JSON string in .NET?