Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Newtonsoft.Json;
- using System.Runtime.Serialization;
- using System.Collections.Generic;
- namespace DeserialisationTest
- {
- [DataContract]
- class RootType
- {
- [DataMember(IsRequired = true)]
- public Dictionary<string, FooType> things { get; set; }
- }
- [DataContract]
- class FooType
- {
- [DataMember(IsRequired = true)]
- public double x { get; set; }
- [DataMember(IsRequired = true)]
- public double y { get; set; }
- [DataMember(IsRequired = true)]
- public List<int> a { get; set; }
- //[DataMember(Name = "z")]
- //public List<bool> zList { get; set; }
- [DataMember(Name = "z")]
- public Dictionary<string, bool> zDict { get; set; }
- }
- class MainClass
- {
- public static void Main (string[] args)
- {
- 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]}}}";
- string jsonStringD = "{\"things\":{\"1\":{\"x\":123.45,\"y\":678.9,\"z\":{\"2\":true},\"a\":[1,2]}}}";
- string jsonStringL = "{\"things\":{\"2\":{\"x\":1414.23,\"y\":5656.78,\"z\":[true],\"a\":[3]}}}";
- RootType rt = JsonConvert.DeserializeObject<RootType>(jsonStringD);
- Console.WriteLine (rt.things["1"].zDict["2"]);
- //Console.WriteLine (rt.things["2"].zList[0]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement