包裹JSON时,使用JavaScriptSerializer反序列化对象 [英] Use JavaScriptSerializer to Deserialize object when JSON is wrapped
问题描述
JSON示例:
{
"1":{
"guid":"8a40135230891fa70130891ff1000009",
"portalId":23832
}
}
我用来反序列化的代码是:
The code that I am using to deserialize is:
var serializer = new JavaScriptSerializer();
var lead = serializer.Deserialize<WebHookResponse>([json]);
我与反序列化代码一起使用的代码是:
The code that I am using with the deserialization code is:
public class WebHookResponse
{
public HubspotRecord Record { get; set; }
}
省略了组成HubspotRecord的代码.当我对未包装在"1"后面的json数据运行该过程时,它可以完美运行.
The code that makes up the HubspotRecord is left out. When I run the process on json data that is not wrapped behind a "1" it works perfectly.
当前,我在想我的问题是json包装在"1"中.由于命名约定,我无法将属性与JavaScriptSerializer一起使用,也无法创建标记为"1"的根元素.
有什么想法吗?
推荐答案
尝试对它进行反序列化,请使用Dictionary<string, HubspotRecord>
或Dictionary<string, WebHookResponse>
.我不能说我经常使用JavaScriptSerializer
,但是我希望它可以在数据库中创建一个键为"1"且具有适当值的条目.无论如何都值得一试:)
Try giving deserializing it do a Dictionary<string, HubspotRecord>
or a Dictionary<string, WebHookResponse>
. I can't say I've used the JavaScriptSerializer
much, but I'd expect it to create an entry in the database with a key of "1" and the appropriate value. Worth a try, anyway :)
或者,您可能希望将其加载到 Json.NET 中,成为类似DOM的结构,然后反序列化你真正想要的那一点.
Alternatively, you might want to load it in Json.NET into a DOM-like structure, and just deserialize the bit you really want.
这篇关于包裹JSON时,使用JavaScriptSerializer反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!