简体   繁体   English

使用Json.net反序列化时出现“意外的令牌:StartObject”

[英]“Unexpected token: StartObject” when Deserializing with Json.net

I have the following JSON that a C# WebClient returned: 我有一个C#WebClient返回的以下JSON:

"\n\n\n{\n \"resultCount\":1,\n \"results\": [\n{\"wrapperType\":\"artist\", \"artistType\":\"Artist\", \"artistName\":\"Jack Johnson\", \"artistLinkUrl\":\"http://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4\", \"artistId\":909253, \"amgArtistId\":468749, \"primaryGenreName\":\"Rock\", \"primaryGenreId\":21}]\n}\n\n\n"

or, more clearly: 或者,更清楚地说:

{
   "resultCount ":1,
   "results ":[
      {
         "wrapperType ":"artist ",
         "artistType ":"Artist ",
         "artistName ":"Jack Johnson ",
         "artistLinkUrl ":"http://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4 ",
         "artistId ":909253,
         "amgArtistId ":468749,
         "primaryGenreName ":"Rock ",
         "primaryGenreId ":21
      }
   ]
}

I've tried deserializing this to a class, like so: 我已经尝试将其反序列化为类,如下所示:

 thejsonresult = JsonConvert.DeserializeObject<JsonResult>(WebRequest.Json);

but received the following error: 但收到以下错误:

Error reading string. 读取字符串出错。 Unexpected token: StartObject. 意外的令牌:StartObject。 Line 7, position 2. 第7行,第2位。

I'm pretty lost and can't find any documentation on this. 我很迷茫,找不到任何关于此的文件。 Anyone got a clue? 有人知道吗?

I believe the problem is actually to be found in the class to which you are attempting to deserialise. 我相信问题实际上是在你试图反序列化的类中找到的。

The wrapper property you have is not being deserialised to correctly. 您拥有的包装器属性未正确反序列化。 If it is an object in your class, then it should work, but if it is a string, which I am guessing, then the Json deserialiser will try to deserialise it and find it's not a string. 如果它是你班级中的一个对象,那么它应该可以工作,但如果它是一个字符串,我猜,那么Json反序列化器将尝试反序列化它并发现它不是一个字符串。

This doesn't throw any exceptions for me in LINQPad: 这不会在LINQPad中为我抛出任何异常:

JsonConvert.DeserializeObject<JsonResult>("\n\n\n{\n \"resultCount\":1,\n \"results\": [\n{\"wrapperType\":\"artist\", \"artistType\":\"Artist\", \"artistName\":\"Jack Johnson\", \"artistLinkUrl\":\"http://itunes.apple.com/us/artist/jack-johnson/id909253?uo=4\", \"artistId\":909253, \"amgArtistId\":468749, \"primaryGenreName\":\"Rock\", \"primaryGenreId\":21}]\n}\n\n\n")

If you're getting different results, you may want to try a different version of JSON.NET to see if it's a bug. 如果您得到不同的结果,您可能想尝试使用不同版本的JSON.NET来查看它是否是一个错误。

I recently ran into the same type of de-serializing exception, “Unexpected token: StartObject”, using the string from the serialization result. 我最近使用序列化结果中的字符串遇到了相同类型的反序列化异常“Unexpected token:StartObject”。

Seems the property declaration of the target cannot be the same name as the object type. 似乎目标的属性声明不能与对象类型同名。 In my case the object being de-serialized had a property of ZipCode which was a class of ZipCode. 在我的例子中,被反序列化的对象具有ZipCode的属性,ZipCode是一类ZipCode。

Ultimately, changing the property name to something other than the class name resolved the exception. 最终,将属性名称更改为类名以外的其他名称可解决该异常。

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

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