JSON 包装时使用 JavaScriptSerializer 反序列化对象
JSON 示例:
{
"1":{
"guid":"8a40135230891fa70130891ff1000009",
"portalId":23832
}
}
我用于反序列化的代码是:
var serializer = new JavaScriptSerializer();
var lead = serializer.Deserialize<WebHookResponse>([json]);
我用于反序列化代码的代码是:
public class WebHookResponse
{
public HubspotRecord Record { get; set; }
}
构成 HubspotRecord 的代码被省略。当我对未包含在“1”后面的 json 数据运行该过程时,它工作得很好。
目前我认为我的问题是 json 被包裹在“1”中。我无法将属性与 JavaScriptSerializer 一起使用,并且由于命名约定,我无法创建标记为“1”的根元素。
有什么想法吗?
Sample of 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; }
}
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.
Currently I am thinking my problem is that the json is wrapped in the "1". I cannot use attributes with the JavaScriptSerializer and I cannot create a root element marked as "1" because of naming conventions.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试对其进行反序列化,执行
Dictionary
或Dictionary
。我不能说我经常使用JavaScriptSerializer
,但我希望它在数据库中创建一个键为“1”和适当值的条目。无论如何,值得一试:)或者,您可能希望将 Json.NET 中的它加载到类似 DOM 的结构中,然后反序列化您真正想要的部分。
Try giving deserializing it do a
Dictionary<string, HubspotRecord>
or aDictionary<string, WebHookResponse>
. I can't say I've used theJavaScriptSerializer
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 :)Alternatively, you might want to load it in Json.NET into a DOM-like structure, and just deserialize the bit you really want.