-2

I am new to using json.net but I am looking to parse the following in to an object

"Properties" :
                                {
                                    "Source": "House",
                                    "Width": 312,
                                    "Height": 190
                                    "ExternalLink": null,
                                    "Link": "#"
                                }

Is is possible to parse the object above in to a IDictionary where the key is for example "Source" and value is "House". I am working with Newtownsoft json.net library.

amateur
  • 43,371
  • 65
  • 192
  • 320
  • As example of numerous questions found by a simple search: http://stackoverflow.com/questions/1207731/how-can-i-deserialize-json-to-a-simple-dictionarystring-string-in-asp-net –  Jan 12 '13 at 03:47
  • (This question gets slightly more interesting - [but still answered](http://stackoverflow.com/questions/6416017/json-net-deserializing-nested-dictionaries/6417753) - when dealing with *nested* structure deserialization rules, but such is not specified.) –  Jan 12 '13 at 03:51
  • Thanks for the info. If the block above was in the middle of a larger document, how could I work it? – amateur Jan 12 '13 at 03:53
  • Look at the [LINQ](http://stackoverflow.com/questions/4749639/deserializing-json-to-net-object-using-newtonsoft-or-linq-to-json-maybe) or `dynamic` use with Json.NET. Alternatively, if the larger structure can be turned into a POCO, such that `Dictionary Properties { get; set; }` is an appropriately attributed member, it will automatically deserialize into the desired type. –  Jan 12 '13 at 03:54
  • Will this work when parsing the entire document? – amateur Jan 12 '13 at 03:55
  • The nested structure approach would also work (no `dynamic` required with Dictionary); but I wouldn't use that unless such was the end-goal. –  Jan 12 '13 at 03:59
  • @R0MANARMY `null` is a supported [JSON value](http://json.org) and is also representable by `null` in .NET. The NewtonSoft Json.NET library will parse the nested JSON fine as either `Dictionary` or `Dictionary` as both `string` (and `object`) types can assume a `null` value. See the linked answers which also discuss other ways that Json.NET can handle this scenario. –  Jan 12 '13 at 06:05

1 Answers1

0

you cat try somethink like this but you need to have a class you Deserialize to

class Data{
  public string Name;
  public object Value;
}
 Dictionary<string, object> dictionary = JsonConvert.DeserializeObject<Data>(yourjson.ToDictionary(x=>x.Name, y=>y.Value));
COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • Something looks off. Surely `yourjson.ToDictionary` is not appropriate: the input starts *as* a string. –  Jan 12 '13 at 03:45