0

"responseCode": 文字列

"responseMessage": 文字列

"responseBody": { "会話": [

{

"conversationId": 文字列,

「状態」: 文字列、

"conversationType": 文字列,

"mediaType": 列挙型,

"開始日":整数,

「期間」: 整数、

"tags":[{ "tagName":String,

"tagType":文字列,

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

Responsejson のルートを表すオブジェクトのタイプはどこになりますか。

詳細については、DataContractJsonSerializer クラスの MSDN ページを参照してください

于 2010-09-22T11:21:49.443 に答える