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); }
}
如何获得&#39; o-data&#39;的内部列表的KVP。
目前它仅因o-data
而循环一次。我想得到o-data
的内部列表并循环遍历。
答案 0 :(得分:2)
需要一些铸件:
1)将kvp.Value
强制转换为object[]
,因为“o-data”包含数组
2)将此数组的第一项投射到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"];
}