Click or drag to resize
Json.NETParsing 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
1string json = @"{
2  CPU: 'Intel',
3  Drives: [
4    'DVD read/writer',
5    '500 gigabyte hard drive'
6  ]
7}";
8
9JObject o = JObject.Parse(json);
Parsing a JSON Array from text
1string json = @"[
2  'Small',
3  'Medium',
4  'Large'
5]";
6
7JArray 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
1using (StreamReader reader = File.OpenText(@"c:\person.json"))
2{
3    JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader));
4    // do stuff
5}
See Also