Deserialize array values to .NET properties using DataContractJsonSerializer

后端 未结 2 557
抹茶落季
抹茶落季 2020-12-24 09:15

I\'m working with the DataContractJsonSerializer in Silverlight 4 and would like to deserialize the following JSON:

{
    \"collectionname\":\"Books\",
    \         


        
2条回答
  •  感动是毒
    2020-12-24 09:39

    I find your question very interesting. So I have to spent my time in trying to solve the problem. Currently I received an example which can serialize and deserealize JSON data like following:

    {
      "collectionname":"Books",
      "collectionitems":[
        {"book":["12345-67890",201,"Book One"]},
        {"book":["09876-54321",45,"Book Two"]}
      ]
    }
    

    the corresponding code of a small console application:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Security.Permissions;
    
    namespace DataContractJsonSerializer {
        [DataContract]
        class BookCollection {
            [DataMember (Order = 0)]
            public string collectionname { get; set; }
            [DataMember (Order = 1)]
            public List collectionitems { get; set; }
        }
    
        [Serializable]
        [KnownType (typeof (object[]))]
        class Book: ISerializable {
            public string Id { get; set; }
            public int NumberOfPages { get; set; }
            public string Title { get; set; }
    
            public Book () { }
    
            [SecurityPermissionAttribute (SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
            protected Book (SerializationInfo info, StreamingContext context) {
                // called by DataContractJsonSerializer.ReadObject
                Object[] ar = (Object[]) info.GetValue ("book", typeof (object[]));
    
                this.Id = (string)ar[0];
                this.NumberOfPages = (int)ar[1];
                this.Title = (string)ar[2];
            }
    
            [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
            public void GetObjectData (SerializationInfo info, StreamingContext context) {
                // called by DataContractJsonSerializer.WriteObject
                object[] ar = new object[] { (object)this.Id, (object)this.NumberOfPages, (object)this.Title };
                info.AddValue ("book", ar);
            }
        }
    
        class Program {
            static readonly string testJSONdata = "{\"collectionname\":\"Books\",\"collectionitems\":[{\"book\":[\"12345-67890\",201,\"Book One\"]},{\"book\":[\"09876-54321\",45,\"Book Two\"]}]}";
    
            static void Main (string[] args) {
                BookCollection test = new BookCollection () {
                    collectionname = "Books",
                    collectionitems = new List {
                        new Book() { Id = "12345-67890", NumberOfPages = 201, Title = "Book One"},
                        new Book() { Id = "09876-54321", NumberOfPages = 45, Title = "Book Two"},
                    }
                };
    
                MemoryStream memoryStream = new MemoryStream ();
                System.Runtime.Serialization.Json.DataContractJsonSerializer ser =
                    new System.Runtime.Serialization.Json.DataContractJsonSerializer (typeof (BookCollection));
                memoryStream.Position = 0;
                ser.WriteObject (memoryStream, test);
    
                memoryStream.Flush();
                memoryStream.Position = 0;
                StreamReader sr = new StreamReader(memoryStream);
                string str = sr.ReadToEnd ();
                Console.WriteLine ("The result of custom serialization:");
                Console.WriteLine (str);
    
                if (String.Compare (testJSONdata, str, StringComparison.Ordinal) != 0) {
                    Console.WriteLine ("Error in serialization: unexpected results.");
                        return;
                }
    
                byte[] jsonDataAsBytes = System.Text.Encoding.GetEncoding ("iso-8859-1").GetBytes (testJSONdata);
                MemoryStream stream = new MemoryStream (jsonDataAsBytes);
                stream.Position = 0;
                BookCollection p2 = (BookCollection)ser.ReadObject (stream);
            }
        }
    }
    

    I don't yet tested this approach under Silverlight 4.

提交回复
热议问题