简体   繁体   中英

Deserializing JSON in C#

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);
}

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);

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM