Parsing JSON |
LINQ to JSON has methods available for parsing JSON from a string or loading JSON directly from a file.
JSON values can be read from a string using Parse(String).
string json = @"{ CPU: 'Intel', Drives: [ 'DVD read/writer', '500 gigabyte hard drive' ] }"; JObject o = JObject.Parse(json);
string json = @"[ 'Small', 'Medium', 'Large' ]"; JArray a = JArray.Parse(json);
JSON can also be loaded directly from a file using ReadFrom(JsonReader).
using (StreamReader reader = File.OpenText(@"c:\person.json")) { JObject o = (JObject)JToken.ReadFrom(new JsonTextReader(reader)); // do stuff }