复杂的 JSON 模式..帮助

发布于 2024-09-24 07:13:21 字数 560 浏览 12 评论 0原文

“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女的英雄梦 2024-10-01 07:13:21

如果您想要(或必须)使用 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

If you want (or have to) to use JavaScriptSerializer the code could look like following:

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);
        }
    }
}

the corresponding JSON data will be look like

{
    "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
                    }
                ]
            }
        ]
    }
}

You can easy modify the code if you decide to use DataContractJsonSerializer.

飘逸的'云 2024-10-01 07:13:21

首先,您可以使用 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 页面类

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

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

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:

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();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文