Skip to content

Latest commit

 

History

History
58 lines (50 loc) · 1.4 KB

QueryJsonSelectToken.md

File metadata and controls

58 lines (50 loc) · 1.4 KB

Querying JSON with SelectToken

This sample loads JSON and then queries values from it using Argon.JToken.SelectToken(System.String).

var o = JObject.Parse(
    """
    {
      'Stores': [
        'Lambton Quay',
        'Willis Street'
      ],
      'Manufacturers': [
        {
          'Name': 'Acme Co',
          'Products': [
            {
              'Name': 'Anvil',
              'Price': 50
            }
          ]
        },
        {
          'Name': 'Contoso',
          'Products': [
            {
              'Name': 'Elbow Grease',
              'Price': 99.95
            },
            {
              'Name': 'Headlight Fluid',
              'Price': 4
            }
          ]
        }
      ]
    }
    """);

var name = (string) o.SelectToken("Manufacturers[0].Name");

Console.WriteLine(name);
// Acme Co

var productPrice = (decimal) o.SelectToken("Manufacturers[0].Products[0].Price");

Console.WriteLine(productPrice);
// 50

var productName = (string) o.SelectToken("Manufacturers[1].Products[0].Name");

Console.WriteLine(productName);
// Elbow Grease

snippet source | anchor