8/24/11

How to Parse JSON Using C# .Net

There are a few libraries you can use to parse json but they can be a pain to initialize and setup when all you need is to grab a specific value given a parameter name and you don't have an array of data.


Here's a function I use for the above scenario:






private string ParseJSON(string getField, string jsonData)
{
string returnValue = "";

returnValue = jsonData.Substring((jsonData.IndexOf(getField) + getField.Length + 3));
if(returnValue.IndexOf(",")<0)
returnValue = returnValue.Substring(0, returnValue.IndexOf("}") - 1);
else
returnValue = returnValue.Substring(0, returnValue.IndexOf(",") - 1);


return returnValue;

}