Click or drag to resize
Json.NETRead JSON with JsonTextReader
 

This sample reads JSON using the JsonTextReader.

Sample
Usage
 1string json = @"{
 2   'CPU': 'Intel',
 3   'PSU': '500W',
 4   'Drives': [
 5     'DVD read/writer'
 6     /*(broken)*/,
 7     '500 gigabyte hard drive',
 8     '200 gigabype hard drive'
 9   ]
10}";
11
12JsonTextReader reader = new JsonTextReader(new StringReader(json));
13while (reader.Read())
14{
15    if (reader.Value != null)
16    {
17        Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
18    }
19    else
20    {
21        Console.WriteLine("Token: {0}", reader.TokenType);
22    }
23}
24
25// Token: StartObject
26// Token: PropertyName, Value: CPU
27// Token: String, Value: Intel
28// Token: PropertyName, Value: PSU
29// Token: String, Value: 500W
30// Token: PropertyName, Value: Drives
31// Token: StartArray
32// Token: String, Value: DVD read/writer
33// Token: Comment, Value: (broken)
34// Token: String, Value: 500 gigabyte hard drive
35// Token: String, Value: 200 gigabype hard drive
36// Token: EndArray
37// Token: EndObject