Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

4 Answers 4

up vote 5 down vote accepted
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();
share|improve this answer
    
Hi .. this method throws an exception when i try to deserialize a json string containing multiple objects. Is there a way to resolve that ? –  Rajesh May 15 at 8:42
    
please ignore my comment above, the JavaScriptSerializer().DeserializeObject() method seems to be working fine . –  Rajesh May 15 at 8:46

You could use JavaScriptSerializer to serialize the json into a dynamic object which would allow you to access the properties via name e.g.

var address = new JavaScriptSerializer().Deserialize<dynamic>(json);
Console.WriteLine(address["postalcode"]);
share|improve this answer

You could also use newtonsoft : http://james.newtonking.com/pages/json-net.aspx

string json = @"{
  ""Name"": ""Apple"",
  ""Expiry"": new Date(1230422400000),
  ""Price"": 3.99,
  ""Sizes"": [
    ""Small"",
    ""Medium"",
    ""Large""
  ]
}";

JObject o = JObject.Parse(json);

string name = (string)o["Name"];
// Apple

JArray sizes = (JArray)o["Sizes"];

string smallest = (string)sizes[0];
// Small

I found another related post : JSON to string array in C#
Lib : http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx

share|improve this answer

It looks like your input string is a JSON string, for which you can use a JSON deserializer if you want. If not you can use regular-expression along with named-groups as the following:

List<string> values = new List<string>();
string pattern = @"\""(?<key>[^\""]+)\""\:\""?(?<value>[^\"",}]+)\""?\,?";
foreach(Match m in Regex.Matches(input, pattern))
{
    if (m.Success)
    {
        values.Add(m.Groups["value"].Value);
        //keys.Add(m.Groups["key"].Value);
    }
}

result = values.ToArray();

Named groups in regular-expression are indicated by (?<group-name>pattern). In the above pattern we have two named groups: key, and value which can be grabbed from the Match object using the Groups indexer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.