JSON analysis in JSON.NET with unknown property names

I have some JSON data that look like this:

{ "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" } } } } 

I am using C # with JSON.NET for data analysis. I was able to get data using this algorithm:

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

The problem is the data field. This field is mainly based on user preferences, and data is only inserted if it is being used. Fields are all customizable, and developers can insert as many as they want, without restrictions. In fact, this is all free form data. Also, as you noticed, they can be nested very far with the data.

I tried to run:

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

which does not work.

How are you going to convert it for use in a C # object?

+7
source share
2 answers

You can access it using the JToken / JArray / JObject . For example, this will display all the keys under the data:

 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); } } } 
+11
source

Json.NET can actually analyze the dynamics if this is useful to you. That means you can do something like.

 dynamic parsedObject = JsonConvert.DeserializeObject("{ test: \"text-value\" }"); parsedObject["test"]; // "text-value" parsedObject.test; // "text-value" parsedObject.notHere; // null 

Edit: May be more appropriate for you to repeat values ​​if you do not know what you are looking for.

 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" } } 
+10
source

All Articles