using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static void Test()
public static void Test0()
foreach (var jsonString in GetJson())
var result = JsonConvert.DeserializeObject<JToken>(jsonString);
var total = (string)result.SelectTokens("results.main[*].item.total").SingleOrDefault() ?? string.Empty;
if (!string.IsNullOrEmpty(total))
Console.WriteLine(total);
public static void Test1()
foreach (var jsonString in GetJson())
var result = JToken.Parse(jsonString);
var total = (string)result.SelectToken("results.main[0].item.total") ?? string.Empty;
if (!string.IsNullOrEmpty(total))
Console.WriteLine(total);
Console.WriteLine("no total");
public static void Test2()
foreach (var jsonString in GetJson())
var result = JToken.Parse(jsonString);
var items = result.SelectTokens("results.main[?(@.item.total)].item");
foreach (var item in items)
var name = (string)item["name"];
var total = (decimal)item["total"];
Console.WriteLine("name = {0}, total = {1}", name, total);
static IEnumerable<string> GetJson()
""copyright"": ""Copyright (c)"",
""date"": ""1900-01-01"",
""copyright"": ""Copyright (c)"",
""date"": ""1900-01-01"",
public static void Main()
Console.WriteLine("Environment version: " + Environment.Version);
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Namespace, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");