sasfrog
sasfrog

Reputation: 2460

Error deserializing JSON that's been converted from XML first

I'm creating a .net app that manages some feeds from various web services. I've just started using JSON.NET and I'm looking to do all my conversion to my .net objects from JSON.

So, I thought for any feed that I can't get as JSON, I'd get the XML and convert it first to JSON, then I could use a single set of methods that process the JSON to do the actual work.

In doing this, I've encountered the following problem: after converting the XML to a JSON string (called myInput) with JsonConvert.SerializeXmlNode(myXMLdoc), I get an error when using JsonConvert.DeserializeObject(Of List(Of Object))(myInput):

Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[System.Object]'. Line 1, position 8.

myInput starts like this:

{"?xml":{"@version":"1.0","@encoding":"UTF-8","@standalone":"yes"},"items":{"@size":"72","item":[{"id":"123456","description": ...

So, my question is specifically what is causing this error, but I'd be happy also to hear whether generally my approach is a good idea (i.e. converting all my input data to JSON before proceeding) - or if there's an established pattern for doing this sort of thing that I would be better learning about and implementing.

Upvotes: 0

Views: 337

Answers (1)

Michal Franc
Michal Franc

Reputation: 1056

I think that the problem is with wrong formatting of your JSON string.

Deserialization is complaining that you can't deserialize a JSON object into a List.

{} - means JSON object = .NET object with properties on it or .NET dictionary

[] - means JSON array = .NET array or list

So you should extract only items from the json string and store them as a '[]'

Upvotes: 1

Related Questions