Created
September 2, 2015 19:30
-
-
Save vendettamit/bf42ec3b08c80c7e217a to your computer and use it in GitHub Desktop.
The code I wrote to answer the parsing JSON to Dynamic Expando object http://stackoverflow.com/questions/32335825/deserialized-values-always-return-null-restsharp-get-request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DynamicParserJson | |
{ | |
public static object ConvertJTokenToObject(JToken token) | |
{ | |
if (token is JValue) | |
{ | |
return ((JValue)token).Value; | |
} | |
if (token is JObject) | |
{ | |
ExpandoObject expando = new ExpandoObject(); | |
(from childToken in ((JToken)token) where childToken is JProperty select childToken as JProperty).ToList().ForEach(property => | |
{ | |
((IDictionary<string, object>)expando).Add(property.Name, ConvertJTokenToObject(property.Value)); | |
}); | |
return expando; | |
} | |
if (token is JArray) | |
{ | |
object[] array = new object[((JArray)token).Count]; | |
int index = 0; | |
foreach (JToken arrayItem in ((JArray)token)) | |
{ | |
array[index] = ConvertJTokenToObject(arrayItem); | |
index++; | |
} | |
return array; | |
} | |
throw new ArgumentException(string.Format("Unknown token type '{0}'", token.GetType()), "token"); | |
} | |
public static void DynamicParserViaExpando() | |
{ | |
dynamic result = JObject.Parse(JsonDataTest.SampleJson); | |
var expando = ConvertJTokenToObject(result) as IDictionary<string, object>; | |
List<SingleCardDetails> cardDetails = new List<SingleCardDetails>(); | |
foreach (var item in expando) | |
{ | |
if (item.Value is ExpandoObject) | |
{ | |
var store = item.Value as IDictionary<string, object>; | |
if (!store.Keys.Contains("bizcardData")) // check properties are on first level | |
{ | |
cardDetails.Add(TryGetData(store)); | |
} | |
else // check second level where contact details are under bizcardData | |
{ | |
foreach (var level2 in item.Value as IDictionary<string, object>) | |
{ | |
if (level2.Value is ExpandoObject) | |
{ | |
var storeLevel2 = level2.Value as IDictionary<string, object>; | |
cardDetails.Add(TryGetData(storeLevel2)); | |
} | |
} | |
} | |
} | |
} | |
} | |
private static SingleCardDetails TryGetData(IDictionary<string, object> store) | |
{ | |
object name; | |
object company; | |
object designation; | |
object email; | |
object phone; | |
store.TryGetValue("name", out name); // check properties are on first level | |
store.TryGetValue("company", out company); | |
store.TryGetValue("designation", out designation); | |
store.TryGetValue("email", out email); | |
store.TryGetValue("phone", out phone); | |
return new SingleCardDetails | |
{ | |
Name = Convert.ToString(name), | |
Company = Convert.ToString(company), | |
Email = Convert.ToString(email), | |
Designation = Convert.ToString(designation), | |
Phone = Convert.ToString(phone) | |
}; | |
} | |
} | |
public class JsonDataTest | |
{ | |
public const string SampleJson = @"{ " + | |
"\"-JxpUA1eC_I3miJrdGGs\": {" + | |
"\"company\": \"Microsoft\"," + | |
"\"designation\": \"Chief Executive officer\"," + | |
"\"email\": \"white@microsoft.com\"," + | |
"\"name\": \"Nathan White\"," + | |
"\"phone\": \"51342212333\"" + | |
"}," + | |
"\"-Jy5dki5KkgyxQBuDwNI\": {" + | |
"\"bizcardData\": {" + | |
"\"company\": \"Spotify\"," + | |
"\"designation\": \"Designer\"," + | |
"\"email\": \"komarapa@spotify.com\"," + | |
"\"name\": \"Phani Komaravolu\"," + | |
"\"phone\": \"5135921240\"" + | |
"}," + | |
"\"transData\": {" + | |
"\"date\": \"15-08-31\"," + | |
"\"location\": \"39.1395996,-84.5295417\"," + | |
"\"tag\": \"sender\"," + | |
"\"time\": \"22:05:38\"" + | |
"}" + | |
"}}"; | |
} | |
public class SingleCardDetails | |
{ | |
public string Name { get; set; } | |
public string Company { get; set; } | |
public string Designation { get; set; } | |
public string Email { get; set; } | |
public string Phone { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment