反序列化多维Json响应

时间:2013-10-19 17:08:41

标签: c# json deserialization

我目前仍然坚持使用JSON反序列化。

这是两种类型的JSON,我可以收到:

{

"code": 0,
"response": {
    "isFollowing": false,
    "isFollowedBy": false,
    "connections": {
        "google": {
            "url": "",
            "id": "c35f4e",
            "name": "jean"
        },
        "facebook": {
            "url": "https://www.facebook.com/",
            "id": "1000064139",
            "name": "jean mic"
        }
    },
    "isPrimary": true,
    "id": "780",
    "location": "",
    "isPrivate": false,
    "joinedAt": "2013-10-18T16:04:09",
    "username": "jeandavid",
    "numLikesReceived": 0,
    "about": "",
    "name": "jean",
    "url": "",
    "profileUrl": "",
    "reputation": ,
    "avatar": {
        "small": {
            "permalink": "https://picture.jpg",
            "cache": "https://picture.jpg"
        },
        "isCustom": false,
        "permalink": "https://picture.jpg",
        "cache": "/noavatar9.png",
        "large": {
            "permalink": "w",
            "cache": "https://picture.jpg"
        }
    },
    "isAnonymous": false
}

}

这一个:

{

"response": [
    {
        "uid": 2017156,
        "first_name": "David",
        "last_name": "Jean",
        "sex": 1,
        "nickname": "",
        "bdate": "12.12.1990",
        "photo_medium": "img.jpg"
    }
]

}

一开始,我使用:

Dictionary<string, string> deserializedObj = JsonConvert.DeserializeObject<Dictionary<string, string>>(response);

但它只适用于一维Json数据。

所以在浏览Google之后,我尝试使用:

JArray jArr = (JArray)JsonConvert.DeserializeObject(response);
foreach (var item in jArr)
{
Console.WriteLine(item);
}

但是我收到了这个例外:

  

无法将“Newtonsoft.Json.Linq.JObject”类型的对象强制转换为“Newtonsoft.Json.Linq.JArray”。

对于第一个JSON数据,我想获取Google和Facebook数据以及用户名,声誉,头像,用户ID,...

感谢您的帮助!!

0 个答案:

没有答案