3

I have the following JSON that a C# WebClient returned:

"\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. Line 7, position 2.

I'm pretty lost and can't find any documentation on this. Anyone got a clue?

3
  • The " at the start/end of that JSON string make it invalid. Remove those, and you've got a valid JSON string. Commented May 8, 2012 at 19:44
  • 1
    the " is just because it's a string in C#, it's not part of the original JSON Commented May 8, 2012 at 19:48
  • Well, without the " it parses fine over at jsonlint.org. So... weird. Commented May 8, 2012 at 20:48

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

Comments

4

I recently ran into the same type of de-serializing exception, “Unexpected token: StartObject”, using the string from the serialization result.

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.

Ultimately, changing the property name to something other than the class name resolved the exception.

Comments

3

This doesn't throw any exceptions for me in 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.

5 Comments

Tried using DataContractDesereializer and it worked, so probably is a JSON.Net bug.
@jbkkd: Are you using the latest release build of JSON.NET? If so, please take a moment to submit a bug report. If not, you should consider upgrading.
Using the latest version. I will indeed submit a bug
James(author of JSON.net) closed the bug, stating it's not valid JSON and that I should read the documentation, with no real explanation. Read the full bug documentation here: json.codeplex.com/workitem/22759
@jbkkd: I'm sorry to hear that. He probably gets inundated with similar bug reports, and 99.9% of them are from people who used invalid JSON, so that's his default reply. However, I would reiterate that I am having no difficulty at all using the latest version of JSON.NET to deserialize your JSON string. Can you provide standalone sample C# code that reproduces this issue?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.