0

我目前停留在 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,...

谢谢你的帮助!!

4

0 回答 0