public void test_1731_json_loads() // For Json.Net, it is JValue.Parse { //// test_string_formatting shows which strings can parse, which do not //// This string comes from python, but JValue.Parse fails if using @ formatting #pragma warning disable CS0219 // Variable is assigned but its value is never used var b_string = @"\n\n\n{\n ""resultCount"":25,\n ""results"": [\n{""wrapperType"":""track"", ""kind"":""podcast"", ""collectionId"":10892}]}"; //// After removing the \ns, they work var c_string = @"{""resultCount"":25,""results"":[{""wrapperType"":""track"",""kind"":""podcast"",""collectionId"":10892}]}"; #pragma warning restore CS0219 // Variable is assigned but its value is never used //// Or just use the string without @ formatting // 1. Start with a json encoded string, including \n var a_string = "\n\n\n{\n \"resultCount\":25,\n \"results\": [\n{\"wrapperType\":\"track\", \"kind\":\"podcast\", \"collectionId\":10892}]}"; output.WriteLine($"1 a_string:\n{a_string}\n:a_string"); // 2. Use JValue.Parse dynamic d = JValue.Parse(a_string); string s = d.GetType().ToString(); output.WriteLine($"2 type: {s}"); //// d.GetType().ToString().Should().Be("Newtonsoft.Json.Linq.JObject"); d is dynamic, so its elements needs to be assigned to concrete type s.Should().Be("Newtonsoft.Json.Linq.JObject"); // 3. d is dynamic, cannot access dictionary keys directly output.WriteLine($"3 d: {d.ToString()}"); // 4. each data is considered as a JToken JContainer jc = d; var ejc = jc.AsJEnumerable(); foreach (var k in ejc) { output.WriteLine($"4 {k}"); } // 5. access results dictionary JContainer results = d.results; for (int i = 0; i < results.Count; ++i) { output.WriteLine($"5 {i} -> {results[i]}"); } // 6. access dictionary element int r = d.resultCount; int r2 = d["resultCount"]; output.WriteLine($"6 resultCount: {r}"); r.Should().Be(25); r2.Should().Be(25); }// fact