0

“响应代码”:字符串

“响应消息”:字符串

“responseBody”:{“对话”:[

{

“conversationId”:字符串,

“状态”:字符串,

“对话类型”:字符串,

“媒体类型”:枚举,

“开始日期”:整数,

“持续时间”:整数,

“标签”:[{ “标签名称”:字符串,

“标签类型”:字符串,

“tagCreateDate”:整数,

“tagOffset”:整数

}], ]}

此模式仍在继续,但我关于第一部分的问题适用于其余部分......

如何将基于此架构的 JSON 响应反序列化为 .NET 对象?.NET 对象会是什么样子?

还有另一种阅读方式吗?(像 .NET 数据集类型的方式?)

谢谢。罗伊。

4

2 回答 2

1

如果您想(或必须)使用JavaScriptSerializer,代码可能如下所示:

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace JsonSer {
    public class MyTag {
        public string tagName { get; set; }
        public string tagType { get; set; }
        public long tagCreateDate { get; set; }
        public int tagOffset { get; set; }
    }
    public enum MyMedia {
        Diskette,
        UsbStick,
        Disk,
        Internet
    }
    public class MyConversation {
        public string conversationId { get; set; }
        public string state { get; set; }
        public string conversationType { get; set; }
        public MyMedia mediaType { get; set; }
        public long startDate { get; set; }
        public int duration { get; set; }
        public List<MyTag> tags { get; set; }
    }
    public class MyConversations {
        public List<MyConversation> conversations { get; set; }
    }
    public class MyData {
        public string responseCode { get; set; }
        public string responseMessage { get; set; }
        public MyConversations responseBody { get; set; }
    }
    class Program {
        static void Main (string[] args) {
            MyData data = new MyData () {
                responseCode = "200",
                responseMessage = "OK",
                responseBody = new MyConversations () {
                    conversations = new List<MyConversation> () {
                         new MyConversation() {
                             conversationId = "conversation1",
                             state = "state1",
                             conversationType = "per JSON",
                             mediaType = MyMedia.Internet,
                             startDate = DateTime.Now.Ticks,
                             duration = 12345,
                             tags = new List<MyTag>() {
                                 new MyTag() {
                                     tagName = "tagName1",
                                     tagType = "tagType1",
                                     tagCreateDate = DateTime.Now.Ticks,
                                     tagOffset = 1
                                 }
                             }
                         }
                     }
                }
            };

            Console.WriteLine ("The original data has responseCode={0}", data.responseMessage);
            JavaScriptSerializer serializer = new JavaScriptSerializer ();
            string json = serializer.Serialize (data);
            Console.WriteLine ("Data serialized with respect of JavaScriptSerializer:");
            Console.WriteLine (json);
            MyData d = (MyData)serializer.Deserialize<MyData> (json);
            Console.WriteLine ("After deserialization responseCode={0}", d.responseMessage);
        }
    }
}

相应的 JSON 数据将如下所示

{
    "responseCode": "200",
    "responseMessage": "OK",
    "responseBody": {
        "conversations": [
            {
                "conversationId": "conversation1",
                "state": "state1",
                "conversationType": "per JSON",
                "mediaType": 3,
                "startDate": 634207605160873419,
                "duration": 12345,
                "tags": [
                    {
                        "tagName": "tagName1",
                        "tagType": "tagType1",
                        "tagCreateDate": 634207605160883420,
                        "tagOffset": 1
                    }
                ]
            }
        ]
    }
}

如果您决定使用DataContractJsonSerializer ,您可以轻松修改代码。

于 2010-09-22T12:02:17.470 回答
0

首先,您可以使用http://jsbeautifier.org/美化所有 JSON以使其更具可读性,然后我知道的唯一方法就是逐步检查每个属性并为它们创建类。您应该为类添加 [DataContract] 属性,为属性添加 [DataMember] 属性。

例子

[DataContract]
public class Response{
    [DataMember]
    public string responseCode {get;set;}
    [DataMember]
    public string responseMessage {get;set;}
    [DataMember]
    public ResponseBody responseBody {get;set;}
}

自动生成这些类

XMLSerialization 有替代方案(使用 XSD),但据我所知,到目前为止还没有类似的 json 解决方案。

要最终将 json 反序列化为 .NET 对象,您可以使用以下代码:

Response myResponse = new Person();
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json));
System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(myResponse.GetType());
myResponse = serializer.ReadObject(ms) as Response;
ms.Close();

Response代表 json 根的对象类型在哪里。

有关详细信息,请访问DataContractJsonSerializer 类的 MSDN 页面

于 2010-09-22T11:21:49.443 回答