- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 .NET for WinRT (C#) 中,我想将 JSON 字符串反序列化为 Dictionary<string, object>
,其中字典值稍后可以转换为实际类型。JSON 字符串可以包含一个对象层次结构,我希望在 Dictionary<string, object>
中有子对象
这是它应该能够处理的示例 JSON:
{
"Name":"John Smith",
"Age":42,
"Parent":
{
"Name":"Brian Smith",
"Age":65,
"Parent":
{
"Name":"James Smith",
"Age":87,
}
}
}
我尝试使用 DataContractJsonSerializer 这样做:
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings();
settings.UseSimpleDictionaryFormat = true;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Dictionary<string, object>), settings);
Dictionary<string, object> results = (Dictionary<string, object>)serializer.ReadObject(ms);
}
这实际上在第一层工作得很好,但是 “Parent” 只是一个不能转换为 Dictionary<string, object>
的对象:
Dictionary<string, object> parent = (Dictionary<string, object>)results["Parent"];
Cannot cast 'results["Parent"]' (which has an actual type of 'object') to 'System.Collections.Generic.Dictionary<string,object>'
然后我尝试使用 Json.NET 但子对象是 JObject 本身是 IDictionary<string, JToken>
,这迫使我遍历整个层次结构并再次转换它们。
有人知道如何使用现有的序列化器解决这个问题吗?
编辑
我正在使用 Dictionary<string, object>
因为我的对象因一个服务器调用而异(例如,“Id” 属性可能是 “id”、*“cust_id”* 或 “customerId” 取决于请求)并且因为我的应用不是唯一使用这些服务的应用,所以我不能改变它,至少现在是这样。
因此,我发现在这种情况下使用DataContractAttribute 和DataMemberAttribute 很不方便。相反,我想将所有内容存储在通用字典中,并有一个强类型属性“Id”,它在字典中查找“id”、“cust_id”或“customerId”,使其对 UI 透明。
这个系统与 JSON.NET 配合得很好,但是如果服务器返回一个对象层次结构,子对象将作为 JObjects 存储在我的字典而不是另一个字典中。
总而言之,我正在寻找一个有效的系统来将对象层次结构转换为 Dictionary<string, object>
的层次结构使用 WinRT 中可用的 JSON 序列化器。
最佳答案
我正在使用 JSON.NET 库和 ExpandoObject 类的组合解决 WinRT 应用程序中的同一问题。该库能够很好地将 JSON 数据反序列化为实现 IDictionary 的 ExpandoObjects。 ExpandoObject 的键值对的值很容易被视为另一个 ExpandoObject。
这是我使用的方法,适用于您的样本:
void LoadJSONData()
{
string testData = "{ \"Name\":\"John Smith\", \"Age\":42, \"Parent\": { \"Name\":\"Brian Smith\", \"Age\":65, \"Parent\": { \"Name\":\"James Smith\", \"Age\":87, } } }";
ExpandoObject dataObj = JsonConvert.DeserializeObject<ExpandoObject>(testData, new ExpandoObjectConverter());
// Grab the parent object directly (if it exists) and treat as ExpandoObject
var parentElement = dataObj.Where(el => el.Key == "Parent").FirstOrDefault();
if (parentElement.Value != null && parentElement.Value is ExpandoObject)
{
ExpandoObject parentObj = (ExpandoObject)parentElement.Value;
// do something with the parent object...
}
// Alternately, iterate through the properties of the expando
foreach (var property in (IDictionary<String, Object>)dataObj)
{
if (property.Key == "Parent" && property.Value != null && property.Value is ExpandoObject)
{
foreach (var parentProp in (ExpandoObject)property.Value)
{
// do something with the properties in the parent expando
}
}
}
}
关于c# - 将 JSON 对象层次结构反序列化为 Dictionary<string, object> 的层次结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13631208/
我正在尝试将多个水平链接的 Button 和 TextView 垂直链接为 View 集,但仍保持平面 View 层次结构。这是我的初始布局和代码:
到目前为止,我已经在Google BigQuery上训练了几种模型,目前我需要查看模型的外观(即架构,损失函数等)。 有没有办法获取这些信息? 最佳答案 仔细阅读文档后,我可以说该功能尚不存在。我什至
本文实例讲述了PHP实现二叉树深度优先遍历(前序、中序、后序)和广度优先遍历(层次)。分享给大家供大家参考,具体如下: 前言: 深度优先遍历:对每一个可能的分支路径深入到不能再深入为止,而且每个
我是一名优秀的程序员,十分优秀!