I have trying to convert zoho forms reponse to c# object. The response json contains dynamic key with nested array objects.
Sample response output from zoho:
var jsonString = "{\n \"result\": [\n {\n \"726468000000236142\": [\n {\n \"EmailID\": \"[email protected]\",\n \"CreatedTime\": \"1671092494538\" \n }\n ]\n },\n {\n \"726468000000236143\": [\n {\n \"EmailID\": \"[email protected]\",\n \"CreatedTime\": \"16710192494538\" \n }\n ] \n }\n ]\n}";
tried code :
var finalResult = JsonConvert.DeserializeObject<dynamic>(jsonString);
var valueList = finalResult["result"];
foreach(var item in valueList)
{
var json = JsonConvert.SerializeObject(item);
var dictionary = JsonConvert.DeserializeObject<Dictionary<string,object>>(json);
var json2 = JsonConvert.SerializeObject(dictionary);
//var json3 = JsonConvert.SerializeObject(dictionary2);
Console.WriteLine(dictionary["726468000000236142"].selectToken("EmailID"));
}
Fiddle: (Working Version) https://dotnetfiddle.net/CprVGw
CodePudding user response:
For the JSON string you gave, you can change your object class to
public class Root
{
public List<Dictionary<string, List<People>>> Result { get; set; }
}
then you can deserialize it by
var finalResult = JsonConvert.DeserializeObject<Root>(jsonString);
foreach (var item in finalResult.Result)
{
foreach (var key in item.Keys)
{
//726468000000236142
Console.WriteLine(key);
foreach (var people in item[key])
{
Console.WriteLine(people.EmailID);
}
}
}