mdc mdc - 5 months ago 29
JSON Question

Parse this json string to string array c#

Feels like there is a one-two row solution for what I want to do:
Parse a string like this:

"{\"postalcode\":\"12345\",\"postalcity\":\"SOME-CITY\",\"country\":\"UK\",\"box\":false}"


Into something like this:

string[] result = { "12345", "SOME-CITY", "UK", "false" };


Whats the simplest way to do this?

L.B L.B
Answer
string json = "{\"postalcode\":\"12345\",\"postalcity\":\"SOME-CITY\",\"country\":\"UK\",\"box\":false}";

var dict = new JavaScriptSerializer().Deserialize<Dictionary<string,object>>(json);
var postalCode = dict["postalcode"];

//Array is also possible
string[] result = dict.Select(kv => kv.Value.ToString()).ToArray();