0

I want to deserialize a json that I get as result of a REST query (the json string can not be changed) to a dictionary type. The json string looks something like this:

{
 "collection": {
   "useful": true 
   "attributes": {
     "ObjectID": "ObjectID",
     "Name": "Name",
     "FirstID": "FirstID",
     "LastID": "LastID",
     "Count": "5",
     },
   "Type": "Polyline",
   "features": [{
     "attributes": {
        "length": 0.10879009704943393
        "time": 0.3822371137674949,
        "text": "some text",
        "ABC": -2209161600000,
        "Type": "SomeType"
        }
      }]
    }
}

I create boolean property for 'useful' and integer for 'count' etc. but I have a problem with the 'attributes'. As you can see, in each section (and per result) I get different 'attributes'. I need to deserialize them into some generic collection like dictionary or list of KeyValuePair. the problem is, as stated in msdn (here - http://msdn.microsoft.com/en-us/library/bb412170.aspx) "Dictionaries are not a way to work directly with JSON".

How can I do it if so? My application is silverlight 5, .Net 4, VS 2010.

Thanks in advance!

dbc
  • 104,963
  • 20
  • 228
  • 340
user2717436
  • 775
  • 1
  • 10
  • 23
  • You may use Dictionary as a type of your attributes property. – Dmytro Rudenko Nov 19 '13 at 07:52
  • I tried it. this is the exception I get: "The data contract type '...' cannot be deserialized because the member 'Attributes' is not public. Making the member public will fix this error. Alternatively, you can make it internal, and use the InternalsVisibleToAttribute attribute on your assembly in order to enable serialization of internal members - see documentation for more details. Be aware that doing so has certain security implications." – user2717436 Nov 19 '13 at 09:29
  • You have to define attributes property in your classes as public – Dmytro Rudenko Nov 19 '13 at 09:35
  • Sure. this is the property: [DataMember(Name = "attributes", IsRequired = false, EmitDefaultValue = true)] public Dictionary Attributes { get; set; } I get this exception each time I try to deserialize to dictionary, even though the dictionary property is public. – user2717436 Nov 19 '13 at 09:37
  • what kind of json serializer you are using? JSON.NET deserialize jsons like this without any problem. – Dmytro Rudenko Nov 19 '13 at 09:40
  • I am using DataContractJsonSerializer. as much as I know, this is the only serializer available for silverlight. – user2717436 Nov 19 '13 at 09:45
  • [json.net](http://james.newtonking.com/json) successfully works wint Silverlight, and even it has PCL version. But for JsonSerializer you can try use JsonObject as a type for your attributes parameter. I hope it will help you. – Dmytro Rudenko Nov 19 '13 at 09:50
  • Can you please give me an example of using JsonObject? – user2717436 Nov 19 '13 at 13:43
  • just define your Attributes property as JsonObject instead of proposed Dictionary. According [docs](http://msdn.microsoft.com/ru-ru/library/system.json.jsonobject(v=vs.95).aspx) this type asts exactly as dictionary, but has 2 indexers - by string and by int index, so it should helps you to get those annoying attributes ) I hope ) – Dmytro Rudenko Nov 19 '13 at 13:49

0 Answers0