using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public int id { get; set; }
public string first_name { get; set; }
public string _version_ { get; set; }
""response"":{""numFound"":4,""start"":0,""docs"":[
""_version_"":""1558902640594649088""},
""_version_"":""1558902640613523456""},
""_version_"":""1558902640613523457""},
""_version_"":""1558902640613523458""}]
internal static void Test()
Console.WriteLine("Testing full JSON: ");
var test = JToken.Parse(text);
test.SelectToken("response.docs").Parent.Remove();
Console.WriteLine("Testing JSON with \"response.docs\": removed: ");
TestOneLine(test.ToString());
test.SelectToken("response").Parent.Remove();
TestOneLine(test.ToString());
test["response"] = "an invalid response";
TestOneLine(test.ToString());
Console.Write("Verifying that the index operator throws an exception for an index of the wrong type: ");
Assert.Throws(Is.InstanceOf(typeof(Exception)), () => Console.WriteLine(test["response"]["docs"]));
Console.Write(" ... passed.");
static void TestTwoLinesLine(string text)
var jObj = JObject.Parse( text );
var token = jObj.SelectToken("response.docs");
var su = token == null ? null : token.ToObject<Solr_User []>();
Console.WriteLine(JsonConvert.SerializeObject(su, Formatting.Indented));
static void TestOneLine(string text)
var jObj = JObject.Parse( text );
var su = (jObj.SelectToken("response.docs") ?? JValue.CreateNull()).ToObject<Solr_User []>();
Console.WriteLine(JsonConvert.SerializeObject(su, Formatting.Indented));
public static void Main()
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);