如何从使用json数据

时间:2015-12-18 17:43:15

标签: c# json dictionary

我有一个看起来像这样的json文件

{"data":
[0]
    [{"type":"user",
    "id":"e4b167ba-b244-4c32-9566-00492c6d437e",
"attributes":{
    "externalId":"53819703-056B-4505-8D73-FA6049EB2D55",
    "username":"#######",
    "roleId":15478,
    "status":"Active",
    "firstName":"Joe",
    "lastName":"User",
    "email":"Joe.User@example.net",
    "badgeNumber":"609",
    "language":"en_us",
    "createdOn":"2014-01-06T17:02:43.25Z",
    "updatedOn":"2014-03-13T13:36:37.44Z",
    "lastLoginOn":"2014-03-13T13:36:37.193Z",
    "lastInvitedOn":"2014-01-06T17:02:43.44Z",
    "lastActivatedOn":"2014-03-13T13:36:36.983Z",
    "lastDeactivatedOn":null},
"links":{
    "self":"/api/v1/agencies/d191405f-44c0-423f-8574-fa8b8b4746c8/users/e4b167ba-b244-4c32-9566-00492c6d437e"}},
[1]
    {"type":"user",
    "id":"8cd078f8-1987-470f-accf-004d4e3f1f5a",
"attributes":{
    "externalId":"6200A3ED-83F6-4C2A-8542-DD02691CC297",
    "username":"########",
    "roleId":15478,
    "status":"Active",
    "firstName":"JOHN",
    "lastName":"DOE",
    "email":"john.Doe@Example.net",
    "badgeNumber":"6118",
    "language":"en_us",
    "createdOn":"2014-01-06T17:09:35.69Z",
    "updatedOn":"2014-03-13T13:12:46.51Z",
    "lastLoginOn":"2014-03-13T13:12:46.323Z",
    "lastInvitedOn":"2014-01-06T17:09:35.797Z",
    "lastActivatedOn":"2014-03-13T13:12:46.137Z",
    "lastDeactivatedOn":null},
"links":{
    "self":"/api/v1/agencies/d191405f-44c0-423f-8574-fa8b8b4746c8/users/d98e705a-6e15-45eb-8a2a-30f4b542fe79"}},

在属性节点中重复超过1700项。

我需要从这些节点中提取一些数据并填充我试图使用此代码的数据库

string json = response.Content.ReadAsStringAsync().Result;
                JavaScriptSerializer ser = new JavaScriptSerializer();
                var dict = ser.Deserialize<dynamic>(json);
                foreach (var item in dict)
                {
                    Label2.Text = (dict["attributes"]["firstName"]);
                }

只需使用Label.Text作为断点来测试代码。代码在foreach循环中崩溃,异常{“字典中没有给定的密钥。”}

我在这里做错了什么?

由于 佩里

2 个答案:

答案 0 :(得分:2)

我相信你在考虑:

foreach (var item in dict)
{
    Label2.Text = (dict.attributes.firstName);
}

firstName是属性,不是字典键。与attributes相同。

编辑: 请检查以下代码:

foreach (var item in dict)
{
    Label2.Text = (dict["attributes"].firstName);
}

答案 1 :(得分:0)

感谢来自@Kamo的发人深省的问题和评论,我能够找到我的问题的答案,这是其他任何可能遇到此问题的代码。

            {
                //Get the json and convert it to a Dictionary note the new dictionary will contain nested dictionaries
                string json = "However You get it";
                var jss = new JavaScriptSerializer();
                var dict = jss.Deserialize<Dictionary<string, dynamic>>(json);
                //Loop thru the nested Dictionary to get the values you need
                for (int i = 0; i < dict.Values.Sum(x =>x.Count); i++)
                {
                    foreach (var item in dict)
                    {

                        Label2.Text = (dict["data"][i]["attributes"]["firstName"]);

                    }
                }

            }