Tuesday, February 1, 2011

Get Key value pairs out of json – The REGEX way


Recently got challenged to get the Key value pairs out of a json encoded object….
Here’s how my Regex looks…

Regex regex = new Regex("\"(?<Key>[\\w]+)\":\"?(?<Value>([\\s\\w\\d\\.\\\\\\-/:_]+(,[" + ",\\s\\w\\d\\.\\\\\\-/:_]+)?)+)\"?", RegexOptions.Multiline | RegexOptions.Compiled);

// Capture all Matches in the InputText
MatchCollection ms = regex.Matches(_value);

foreach (Match item in ms)
 {
//get key and value per match
           
}
         

  
There was an error in this gadget