将列表<ExpandoObject> 转换为列表<KeyValuePair>
Convert List<ExpandoObject> to List<KeyValuePair>
我目前正在开发 .NET Framework 4.7.2 应用程序。从 Web 服务响应中,我需要将 JSON 数据解析为 List<KeyValuePair<int, Dictionary<string, object>>>
。此数据类型对于进一步的程序流程很重要,我无法更改它。
具有动态属性的 JSON 数据如下所示:
{ "data" : [
{"Id":1, Text:"Test1", coolProp: 213 },
{"Id":2, Text:"Test2"},
{"Id":3, Text:"Test3", otherProp: "cool" },
]}
我尝试了以下编码,但没有成功:
JsonConvert.DeserializeObject<List<KeyValuePair<int, Dictionary<string, object>>>>(Convert.ToString(JObject.Parse(json)["data"]));
另一方面,我可以将 json 转换为 ExpandoObject:
var expando = JsonConvert.DeserializeObject<List<ExpandoObject>>(Convert.ToString(JObject.Parse(json)["data"]));
我考虑写一个私有方法来转换
ExpandoObject 到我的 List<KeyValuePair<int, Dictionary<string, object>>>
.
private KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>> ConvertExpandoToKeyValue(float key, List<ExpandoObject> expando)
{
var result = new KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>>();
// I don't really know how to convert the expando object to the desired data structure
// Moreover I need to put a float key in the structure: 52.2343
return result;
}
ExpandoObject 如下所示:
最终结果 a KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>>
应该是这样的:
你知道如何将ExpandoObject转换成你想要的数据类型,并在开头加一个key吗?
或者,您是否知道将 JSON 数据转换为我想要的数据结构的更好方法?
非常感谢!!
好的,我写了一个解决方案,我只是想和你分享。也许有更好的方法:
private KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>> ConvertExpandoToKeyValue(float key, List<ExpandoObject> expando)
{
var result = new KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>>
(key, new List<KeyValuePair<int, Dictionary<string, object>>>());
for (int i = 0; i < expando.Count; i++)
{
var element = new Dictionary<string, object>(expando[i]);
var propertyValues = new KeyValuePair<int, Dictionary<string, object>>(i, element);
result.Value.Add(propertyValues);
}
return result;
}
我目前正在开发 .NET Framework 4.7.2 应用程序。从 Web 服务响应中,我需要将 JSON 数据解析为 List<KeyValuePair<int, Dictionary<string, object>>>
。此数据类型对于进一步的程序流程很重要,我无法更改它。
具有动态属性的 JSON 数据如下所示:
{ "data" : [
{"Id":1, Text:"Test1", coolProp: 213 },
{"Id":2, Text:"Test2"},
{"Id":3, Text:"Test3", otherProp: "cool" },
]}
我尝试了以下编码,但没有成功:
JsonConvert.DeserializeObject<List<KeyValuePair<int, Dictionary<string, object>>>>(Convert.ToString(JObject.Parse(json)["data"]));
另一方面,我可以将 json 转换为 ExpandoObject:
var expando = JsonConvert.DeserializeObject<List<ExpandoObject>>(Convert.ToString(JObject.Parse(json)["data"]));
我考虑写一个私有方法来转换
ExpandoObject 到我的 List<KeyValuePair<int, Dictionary<string, object>>>
.
private KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>> ConvertExpandoToKeyValue(float key, List<ExpandoObject> expando)
{
var result = new KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>>();
// I don't really know how to convert the expando object to the desired data structure
// Moreover I need to put a float key in the structure: 52.2343
return result;
}
ExpandoObject 如下所示:
最终结果 a KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>>
应该是这样的:
你知道如何将ExpandoObject转换成你想要的数据类型,并在开头加一个key吗?
或者,您是否知道将 JSON 数据转换为我想要的数据结构的更好方法?
非常感谢!!
好的,我写了一个解决方案,我只是想和你分享。也许有更好的方法:
private KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>> ConvertExpandoToKeyValue(float key, List<ExpandoObject> expando)
{
var result = new KeyValuePair<float, List<KeyValuePair<int, Dictionary<string, object>>>>
(key, new List<KeyValuePair<int, Dictionary<string, object>>>());
for (int i = 0; i < expando.Count; i++)
{
var element = new Dictionary<string, object>(expando[i]);
var propertyValues = new KeyValuePair<int, Dictionary<string, object>>(i, element);
result.Value.Add(propertyValues);
}
return result;
}