如何解决从现有的Json流中删除$ type和Id属性?
我正在使用以下代码序列化一个复杂的对象:
var customerResultJson = JsonConvert.SerializeObject(customerResult,CustomerSettings.GetDefaultSettings());
因此,customerResultJson
正在显示$type
和$id
。我无法控制CustomerSettings.GetDefaultSettings
,因为它来自其他项目dll。我想删除那些属性类型。我该如何实现?已尝试以下方法,但是newCustomerResultJson
嵌套对象仍然保留类型和ID。
var customerDeserializeObject = JsonConvert.DeserializeObject(customerResultJson,new JsonSerializerSettings()
{TypeNameHandling = TypeNameHandling.None,Formatting = Formatting.Indented});
var newCustomerResultJson= JsonConvert.SerializeObject(customerDeserializeObject,Formatting = Formatting.Indented});
样本customerResultJson:
{
"$id": "1","$type": "Customer.CustomerAddress,Customer.CustomerAddress.Common,Version=8.0.0.0,Culture=neutral,PublicKeyToken=f291d57f641e84e4","FlatRate": 29.65,"AmountFinanced": 12402.2,"AmountUsed": 12302.2,"TotalPayment": 0,"CustomerAddress": {
"$id": "2","$type": "System.Collections.Generic.Dictionary`2[[System.String,mscorlib,Version=4.0.0.0,PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List`1[[Customer.Customerbject,Drive.Framework,PublicKeyToken=f291d57f641e84e4]],PublicKeyToken=b77a5c561934e089]],PublicKeyToken=b77a5c561934e089","Items": {
"$id": "3","$type": "System.Collections.Generic.List`1[[Customer.Customerbject,Customer.Framework,"$values": []
},"Added": {
"$id": "4","Removed": {
"$id": "5","$values": []
}
}
}
解决方法
这就是我实现目标的方式:在此处发布信息可能会对某人有所帮助。
JObject respJObject = JObject.Parse(JsonResult);
respJObject.Descendants().OfType<JProperty>().Where(attr => attr.Name.StartsWith("$type")).ToList().ForEach(attr => attr.Remove());
以上行也从嵌套属性中删除了类型。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。