如何在C#中读取嵌套的json对象

时间:2018-08-29 13:11:24

标签: c# json

我有json字符串

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "hits": {
        "total": 78,
        "max_score": 1.5,
        "hits": [{
                "_index": "S3",
                "_type": "se",
                "_id": "00CD03",
                "_score": 1.5,
                "contact": {
                    "defaultAddress": {
                        "addressLine1": "AAAAAAAAAAAAAAA",
                        "addressLine2": null,
                        "addressCode": null,
                        "geoLocation": {
                            "lon": "14.1514",
                            "lat": "16.7555"
                        },
                    },
                    "defaultEmail": "aaaa.bbbb@gmail.in",
                    "defaultPhoneNumber": "080 1140 637",
                }
            }, {
                "_index": "S3",
                "_type": "se",
                "_id": "00C3D0-01260-3",
                "_score": 1.5,
                "contact": {
                    "defaultAddress": {
                        "addressLine1": "bbbbbbbbbb 1",
                        "addressLine2": "ggggggggg",
                        "addressCode": null,
                        "geoLocation": {
                            "lon": "97.1511101",
                            "lat": "15.7888899"
                        },
                    },
                    "defaultEmail": "kkkkk@yahoo.de",
                    "defaultPhoneNumber": "0909 140 637",
                }
            }
        ]
    }
}

需要遍历json并在C#中创建列表详细信息。寻找帮助

1 个答案:

答案 0 :(得分:0)

@NLSrini

您可以使用掘金包“ Newtonsoft.Json”。然后,您可以创建代表您的JSON结构的类Object,并可以使用以下方法反序列化:JsonConvert.DeserializeObject<YourClass>(json);

您可以在此处查看更多示例:https://www.newtonsoft.com/json/help/html/SerializingJSON.htm