Click or drag to resize
Json.NET

Parsing JSON

 

LINQ to JSON has methods available for parsing JSON from a string or loading JSON directly from a file.

Parsing JSON text

JSON values can be read from a string using Parse(String).

Parsing a JSON Object from text
string json = @"{
  CPU: 'Intel',
  Drives: [
    'DVD read/writer',
    '500 gigabyte hard drive'
  ]
}";

JObject o = JObject.Parse(json);
Parsing a JSON Array from text
string json = @"[
  'Small',
  'Medium',
  'Large'
]";

JArray a = JArray.Parse(json);
Loading JSON from a file

JSON can also be loaded directly from a file using ReadFrom(JsonReader).

Reading JSON from a file
using (StreamReader reader = File.OpenText(@"c:\person.json"))
{
    JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
    // do stuff
}
See Also