Skip to content

Latest commit

 

History

History
64 lines (49 loc) · 1.9 KB

ParsingLINQtoJSON.md

File metadata and controls

64 lines (49 loc) · 1.9 KB

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 Argon.JToken.Parse(System.String).

var json = """
           {
             CPU: 'Intel',
             Drives: [
               'DVD read/writer',
               '500 gigabyte hard drive'
             ]
           }
           """;

var o = JObject.Parse(json);

snippet source | anchor

var json = """
           [
             'Small',
             'Medium',
             'Large'
           ]
           """;

var a = JArray.Parse(json);

snippet source | anchor

Loading JSON from a file

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

using var reader = File.OpenText(@"c:\person.json");
var o = (JObject) JToken.ReadFrom(new JsonTextReader(reader));
// do stuff

snippet source | anchor

Related Topics

  • LINQtoJSON
  • Argon.JToken.Parse(System.String)
  • Argon.JToken.ReadFrom(Argon.JsonReader)