复杂的JSON模式..帮助

时间:2010-09-22 09:49:51

标签: c# json javascriptserializer

“responseCode”:String

“responseMessage”:String

“responseBody”:{“对话”:[

{

“conversationId”:String,

“state”:String,

“conversationType”:字符串,

“mediaType”:枚举,

“的startDate”:整数,

“持续时间”:整数,

“tags”:[{“tagName”:String,

“tagType”:字符串;

“tagCreateDate”:整数,

“tagOffset”:整数

}], ]}

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

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

还有其他方法可以阅读吗? (比如.NET数据集类型的方式?)

感谢。 Roey。

2 个答案:

答案 0 :(得分: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,则可以轻松修改代码。

答案 1 :(得分: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根的对象类型。

有关详细信息,请访问the MSDN page of the DataContractJsonSerializer class