13

我有一些 JSON 数据,如下所示:

{
   "response":{
   "_token":"StringValue",
   "code":"OK",
   "user":{
     "userid":"2630944",
     "firstname":"John",
     "lastname":"Doe",
     "reference":"999999999",
     "guid":"StringValue",
     "domainid":"99999",
     "username":"jdoe",
     "email":"jdoe@jdoe.edu",
     "passwordquestion":"",
     "flags":"0",
     "lastlogindate":"2013-02-05T17:54:06.31Z",
     "creationdate":"2011-04-15T14:40:07.22Z",
     "version":"3753",
     "data":{
       "aliasname":{
         "$value":"John Doe"
       },
       "smsaddress":{
         "$value":"5555555555@messaging.sprintpcs.com"
       },
       "blti":{
         "hideemail":"false",
         "hidefullname":"false"
       },
       "notify":{
         "grades":{
            "$value":"0"
          },
          "messages":{
            "$value":"1"
          }
       },
       "beta_component_courseplanexpress_1":{
         "$value":"true"
       }
    }
  }
}

我使用 C# 和 JSON.NET 来解析数据。我已经能够使用此算法成功获取数据:

User MyUser = new User();
JToken data = JObject.Parse(json);
MyUser.FirstName = (string) data.SelectToken("response.user.firstname");
//The same for all the other properties.

问题出在data球场上。该字段主要基于用户偏好,并且仅在使用时插入数据。这些字段都是自定义的,开发人员可以不受限制地输入任意数量的字段。本质上,它都是自由形式的数据。此外,正如您所注意到的,它们可以与数据嵌套得很远。

我试图运行:

MyUser.Data = JsonConvert.DeserializeObject<List<JToken>>((string) data.SelectToken("response.user.data");

这是行不通的。

您将如何将其转换为在 C# 对象中使用?

4

2 回答 2

16

您可以通过JToken//方法访问它JArrayJObject例如,这将列出数据下的所有键:

public class StackOverflow_14714085
{
    const string JSON = @"{
                          ""response"": {
                            ""_token"": ""StringValue"",
                            ""code"": ""OK"",
                            ""user"": {
                              ""userid"": ""2630944"",
                              ""firstname"": ""John"",
                              ""lastname"": ""Doe"",
                              ""reference"": ""999999999"",
                              ""guid"": ""StringValue"",
                              ""domainid"": ""99999"",
                              ""username"": ""jdoe"",
                              ""email"": ""jdoe@jdoe.edu"",
                              ""passwordquestion"": """",
                              ""flags"": ""0"",
                              ""lastlogindate"": ""2013-02-05T17:54:06.31Z"",
                              ""creationdate"": ""2011-04-15T14:40:07.22Z"",
                              ""version"": ""3753"",
                              ""data"": {
                                ""aliasname"": {
                                  ""$value"": ""John Doe""
                                },
                                ""smsaddress"": {
                                  ""$value"": ""5555555555@messaging.sprintpcs.com""
                                },
                                ""blti"": {
                                  ""hideemail"": ""false"",
                                  ""hidefullname"": ""false""
                                },
                                ""notify"": {
                                  ""grades"": {
                                    ""$value"": ""0""
                                  },
                                  ""messages"": {
                                    ""$value"": ""1""
                                  }
                                },
                                ""beta_component_courseplanexpress_1"": {
                                  ""$value"": ""true""
                                }
                              }
                            }
                          }
                        }";

    public static void Test()
    {
        var jo = JObject.Parse(JSON);
        var data = (JObject)jo["response"]["user"]["data"];
        foreach (var item in data)
        {
            Console.WriteLine("{0}: {1}", item.Key, item.Value);
        }
    }
}
于 2013-02-05T18:23:11.490 回答
15

如果这对您有用,Json.NET 实际上可以解析为动态。这意味着你可以做类似的事情。

dynamic parsedObject = JsonConvert.DeserializeObject("{ test: \"text-value\" }");

parsedObject["test"]; // "text-value"
parsedObject.test; // "text-value"
parsedObject.notHere; // null

编辑:如果您不知道自己在寻找什么,可能更适合您迭代值。

dynamic parsedObject = JsonConvert.DeserializeObject("{ test: { inner: \"text-value\" } }");
foreach (dynamic entry in parsedObject)
{
    string name = entry.Name; // "test"
    dynamic value = entry.Value; // { inner: "text-value" }
}
于 2013-02-05T18:19:06.833 回答