-1

I know that this question has been asked ad nauseam, but the existing answers haven't been particularly helpful for me. The best is Deserialize JSON into C# dynamic object?, but I don't want to create an object, I want to create a Dictionary.

I serialize my data like so, and I want to deserialize to precisely the same data structure:

    public static string AugDictToJSON(Dictionary<string, List<Dictionary<string, object>>> dict)
    {
        return string.Join(",", dict.Select(
            d => string.Format("{{ \"{0}\" : [ {1} ] }}", d.Key,
                string.Join(",", d.Value.Select(i => SubAugDictToJSON(i)).ToArray())
            )).ToArray());
    }

    public static string SubAugDictToJSON(Dictionary<string, object> dict)
    {
        return string.Join(",", dict.Select(
                d => string.Format("{{ \"{0}\" : \"{1}\" }}", d.Key, d.Value.ToString())
            ).ToArray());
    }

EDIT: Solution is the following (from accepted answer):

public static Dictionary<string, List<Dictionary<string, object>>> JSONToAugDict(string json)
{
    return JsonConvert.DeserializeObject<Dictionary<string, List<Dictionary<string, object>>>>(json);
}
Community
  • 1
  • 1
Sam Grondahl
  • 2,397
  • 2
  • 20
  • 26
  • If you're using v4.5, [`JsonValue.Parse()`](http://msdn.microsoft.com/en-us/library/system.json.jsonvalue.parse.aspx) or its siblings might be close enough. (Not on 4.5 here, so I can't really test code.) – millimoose Oct 01 '12 at 17:15
  • 1
    By the way, your serialisation code is incorrect. JSON doesn't allow for unquoted strings as keys. – millimoose Oct 01 '12 at 17:15
  • `I wonder whether there might be a more elegant way`. Using linq doesn't make your way elegant. – L.B Oct 01 '12 at 17:49
  • 1
    But it does make it readable. – Sam Grondahl Oct 01 '12 at 18:20

2 Answers2

3

I recommend using the NuGet package 'Newtonsoft.Json'.

This package is used by WebAPI for serialization / deserialization and is quite fast. More information can be found here: http://james.newtonking.com/pages/json-net.aspx

var results = JsonConvert.DeserializeObject<dynamic>(json);

Alternatively, as an IDictionary:

var result = JsonConvert.DeserializeObject<IDictionary<string, object>>(json);
Joshua
  • 4,099
  • 25
  • 37
1

I tried something similar using Json.NET for an application I was working on (Taken from my own question: Deserializing json string into an object - Silverlight)

JSON:

{
    "disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability or fitness for any purpose; use at your own risk. Other than that - have fun, and please share/watch/fork if you think data like this should be free!",
    "license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
    "timestamp": 1334183999,
    "base": "USD",
    "rates": {
                "AED": 3.6732,
                "AFN": 48.400002,
                "ALL": 106.669998,
             }
}

Object that will hold the data:

public class ExchangeData
{
    public string disclaimer { get; set; }
    public string license { get; set; }
    public string timestamp { get; set; }
    public string @base { get; set; }
    public Dictionary<string, double> rates { get; set; }
}

To create the data structure:

StreamReader reader = new StreamReader(args.Result);
ExchangeData data = JsonConvert.DeserializeObject<ExchangeData>(reader.ReadToEnd());

Hope it helps!

Community
  • 1
  • 1
Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102