C#子成员中的JSON解析

C#子成员中的JSON解析,c#,json,json.net,C#,Json,Json.net,我试图解析Instagram API中的JSON数据,但在解析子元素时遇到问题。例如,一个Instagram响应如下所示: { "pagination": { "next_url": "https://api.instagram.com/v1/users/273112457/followed-by?access_token=1941825738.97584da.3242609045494207883c900cbbab04b8&cursor=143909084544

我试图解析Instagram API中的JSON数据,但在解析子元素时遇到问题。例如,一个Instagram响应如下所示:

{
    "pagination": {
        "next_url": "https://api.instagram.com/v1/users/273112457/followed-by?access_token=1941825738.97584da.3242609045494207883c900cbbab04b8&cursor=1439090845443",
        "next_cursor": "1439090845443"
    },
    "meta": {
        "code": 200
    },
    "data": [
        {
            "username": "ohdyxl",
            "profile_picture": "https://igcdn-photos-e-a.akamaihd.net/hphotos-ak-xfp1/t51.2885-19/11093019_661322517306044_2019954676_a.jpg",
            "id": "1393044864",
            "full_name": "只有你和我知道"
        },
        {
            "username": "dpetalco_florist",
            "profile_picture": "https://igcdn-photos-a-a.akamaihd.net/hphotos-ak-xtf1/t51.2885-19/11192809_930052080349888_1420093998_a.jpg",
            "id": "1098934333",
            "full_name": "D'petalco florist"
        }
    ]
}
我的代码如下:

dynamic d = JObject.Parse(response);
foreach (var result in d["data"])
{
    string userName = (string)result["username"];
    list.Add(userName);
}
foreach (var res in d["pagination"])
{
    string nexturl = (string)res["next_url"];
    string nextcursor = (string)res["next_cursor"];
}
这一部分工作得很好,但是当我尝试提取分页时,会出现子错误访问错误

我的代码如下:

dynamic d = JObject.Parse(response);
foreach (var result in d["data"])
{
    string userName = (string)result["username"];
    list.Add(userName);
}
foreach (var res in d["pagination"])
{
    string nexturl = (string)res["next_url"];
    string nextcursor = (string)res["next_cursor"];
}

如何从C#中的“分页”中提取下一个_url和下一个_curosr?谢谢。

数据
属性值不同,
分页
属性值不是数组,因此不需要
foreach
循环:

var res = d["pagination"];
string nexturl = (string)res["next_url"];
string nextcursor = (string)res["next_cursor"];
或者不使用中间变量
res

string nexturl = (string)d["pagination"]["next_url"];
string nextcursor = (string)d["pagination"]["next_cursor"];