JSON:
{
"o-data":[
{
"entry 1":"test1",
"entry 2":"test2",
"entry 3":"test3",
"entry 4":"118"
}
]
}
C#:
dynamic dn = dyndata(json);
foreach (KeyValuePair<string, object> kvp in dn)
{
//I would like to be able to get all the inner data for `o-data` like below...
string entry1 = "";// test1
string entry2 = "";// test2
string entry3 = "";// test3
//but instead I get `{[o-data, System.Object[]]}`
string second = kvp.Value.ToString(); // gives my 'System.Object[]'
}
public dynamic dyndata(string json)
{
dynamic dyn1 = new System.Dynamic.ExpandoObject();
try
{
var serializer = new JavaScriptSerializer();
var result = serializer.DeserializeObject(json);
return result);
}
catch { return Tuple.Create(dDummy, false); }
}
How can I the KVP of the inner list for 'o-data'.
Currently it only loop once because of o-data
. I would like to get the inner list of o-data
and loop through that.
Some castings are required:
1) cast the kvp.Value
to object[]
because "o-data" contains an array
2) cast the first item of this array to IDictionary<string, object>
dynamic dn = dyndata(json);
foreach (KeyValuePair<string, object> kvp in dn)
{
var array = ((object[])kvp.Value);
var dictionary = (IDictionary<string, object>) array[0];
var entry1 = dictionary["entry 1"];
}