复杂的 JSON 模式..帮助
“responseCode”:字符串
“responseMessage”:字符串
“responseBody”:{ “conversations”:[
{
“conversationId”:字符串,
“state”:字符串,
“conversationType”:字符串,
“mediaType”:枚举,
“startDate”:整数,
"duration": 整数,
"tags":[{ "tagName":String,
"tagType":String,
"tagCreateDate":Integer,
"tagOffset":Integer
}], ]}
此架构仍在继续,但我关于第一部分的问题适用于其余部分...
如何将基于此架构的 JSON 响应反序列化为 .NET 对象? .NET 对象会是什么样子?
还有另一种阅读方式吗? (就像 .NET 数据集类型一样?)
谢谢。罗伊。
"responseCode": String
"responseMessage": String
"responseBody": { "conversations": [
{
"conversationId": String,
"state": String,
"conversationType": String,
"mediaType": Enum,
"startDate":Integer,
"duration": Integer,
"tags":[{ "tagName":String,
"tagType":String,
"tagCreateDate":Integer,
"tagOffset":Integer
}],
]}
This schema continues, but my question regarding the first section applies to the rest...
How can I deserialize a JSON response based on this schema into .NET objects? what would the .NET object look like?
Is there another way to read it ? (like a .NET Dataset type of way?)
Thanks. Roey.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要(或必须)使用 JavaScriptSerializer 代码可以如下所示:
相应的 JSON 数据将如下所示
如果您决定使用 DataContractJsonSerializer。
If you want (or have to) to use JavaScriptSerializer the code could look like following:
the corresponding JSON data will be look like
You can easy modify the code if you decide to use DataContractJsonSerializer.
首先,您可以使用 http://jsbeautifier.org/ 美化所有 JSON,使其更具可读性,然后我知道的唯一方法是逐步检查每个属性并为它们创建类。您应该为类添加 [DataContract] 属性,为属性添加 [DataMember] 属性。
示例
自动生成这些类
XMLSerialization 有替代方案(使用 XSD),但据我所知,到目前为止,还没有类似的 json 解决方案。
要最终将 json 反序列化为 .NET 对象,您可以使用以下代码:
其中
Response
是表示 json 根的对象类型。有关详细信息,请访问 DataContractJsonSerializer 的 MSDN 页面类。
First you can beautify all your JSON using http://jsbeautifier.org/ to make it more readable, and then the only way I know is to just go through every property step by step and create classes for them. You should add the [DataContract] attribute for classes and the [DataMember] attribute for properties.
Example
Automatic generation of these classes
There are alternatives for XMLSerialization (using XSD) but as far as I know there are no similar solutions for json thus far.
To finally deserialize the json into .NET object you can use the following code:
Where
Response
would be the type of object that represents the root of your json.For more information visit the MSDN page of the DataContractJsonSerializer class.