简体   繁体   English

无法使用 JObject.Parse() 解析 JSON

[英]Unable to parse JSON using JObject.Parse()

Every time I try to parse JSON I get this exception:每次我尝试解析 JSON 时,我都会收到此异常:

    Unhandled exception. Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
   at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json, JsonLoadSettings settings)
   at Newtonsoft.Json.Linq.JObject.Parse(String json)
   at ai.Program.Main(String[] args) in /home/asdf/RiderProjects/ai/ai/Program.cs:line 19

Some of my code我的一些代码

url2 = "img"; 
var client = new RestClient("example.org?url=" + url2);
var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);
dynamic epic = JObject.Parse(response.Content);

string stuff = epic.stuff;

Console.WriteLine(stuff);

That is not a JObject , that is a JArray , you can tell by the square bracket around the entire response.那不是JObject ,而是JArray ,您可以通过整个响应周围的方括号来判断。 You'll need to fix that before anything.你需要先解决这个问题。

JArray arr = JArray.Parse(response.Content);

See my do.netfiddle with your exact object: https://do.netfiddle.net/PGZcay请参阅我的 do.netfiddle 与您的确切 object: https://do.netfiddle.net/PGZcay

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM