0

I have a simple WinForm program that does a WebRequest to a server, the response is a string looking like:

{"a":"foo","b":"bar"}

How do convert this string to a dictionary? (Dictionary<string, string>). Is there a built-in function I can use?

Thomas Wagenaar
  • 6,489
  • 5
  • 30
  • 73
  • possible duplicate of [Deserialize JSON into C# dynamic object?](http://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object) – Rob Sep 12 '15 at 14:38

3 Answers3

6

Use a library like JSON.NET:

var dictionary = JsonConvert.DeserializeObject<IDictionary<string, string>>(json)
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0
var sourve = @"{"a":"foo","b":"bar"}";
Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(source);
Dmitry
  • 477
  • 6
  • 20
0

If you can add two assemblies:

  • System.Web
  • System.Web.Extensions

You can use this code:

string html = "{\"a\":\"foo\",\"b\":\"bar\"}";

JavaScriptSerializer jss = new JavaScriptSerializer();
var dict = jss.Deserialize<Dictionary<string, string>>(html);
Andrzej Budzanowski
  • 1,013
  • 11
  • 17