Advertisement
Guest User

Deserialisation Issues

a guest
Jul 24th, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using Newtonsoft.Json;
  3. using System.Runtime.Serialization;
  4.  
  5. using System.Collections.Generic;
  6.  
  7. namespace DeserialisationTest
  8. {
  9.     [DataContract]
  10.     class RootType
  11.     {
  12.         [DataMember(IsRequired = true)]
  13.         public Dictionary<string, FooType> things { get; set; }
  14.     }
  15.  
  16.     [DataContract]
  17.     class FooType
  18.     {
  19.         [DataMember(IsRequired = true)]
  20.         public double x { get; set; }
  21.  
  22.         [DataMember(IsRequired = true)]
  23.         public double y { get; set; }
  24.  
  25.         [DataMember(IsRequired = true)]
  26.         public List<int> a { get; set; }
  27.  
  28.         //[DataMember(Name = "z")]
  29.         //public List<bool> zList { get; set; }
  30.  
  31.         [DataMember(Name = "z")]
  32.         public Dictionary<string, bool> zDict { get; set; }
  33. }
  34.  
  35.     class MainClass
  36.     {
  37.         public static void Main (string[] args)
  38.         {
  39.  
  40.             string jsonString = "{\"things\":{\"1\":{\"x\":123.45,\"y\":678.9,\"z\":{\"2\":true},\"a\":[1,2]},\"2\":{\"x\":1414.23,\"y\":5656.78,\"z\":[true],\"a\":[3]}}}";
  41.             string jsonStringD = "{\"things\":{\"1\":{\"x\":123.45,\"y\":678.9,\"z\":{\"2\":true},\"a\":[1,2]}}}";
  42.             string jsonStringL = "{\"things\":{\"2\":{\"x\":1414.23,\"y\":5656.78,\"z\":[true],\"a\":[3]}}}";
  43.  
  44.             RootType rt = JsonConvert.DeserializeObject<RootType>(jsonStringD);
  45.             Console.WriteLine (rt.things["1"].zDict["2"]);
  46.             //Console.WriteLine (rt.things["2"].zList[0]);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement