Unexpected state

Oct 10, 2011 at 2:22 PM

I have this json -

[{"a":"2958081","b":"Contact Sport","e":"abc4 news sport recent","f":[{"a":"831081","f":"2011-09-30 23:00:00","g":"2011-10-14 23:00:00"},{"a":"831080","f":"2011-09-23 23:00:00","g":"2011-10-07 23:00:00"}]},{"a":"2959838","b":"ABC National Evening News","e":"news abc4 recent last-chance","f":[{"a":"831928","f":"2011-09-30 23:00:00","g":"2011-10-01 22:00:00"}]}]

which gives me an exception "Unexpected state: Finished. Line 1, position 206." with the following code:

doc = CType(Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json, "root"), XmlDocument)

json is string, doc is XmlDocument

 

Is there a way to handle these repeated array structures? I have shown 2 only, the full JSON file (string) has a few hundred (total 75K bytes). The [ ] enclosing the  whole is stripped out before the code snippet shown.

Oct 11, 2011 at 9:07 PM

Well, that snippet worked fine here, you don't need CType because that method returns XmlDocument.

One note about your JSON string, to deserialize it to XML you need an object, in that case that be something like:

{objects:[{"a":"2958081","b":"Contact Sport","e":"abc4 news sport recent","f":[{"a":"831081","f":"2011-09-30 23:00:00","g":"2011-10-14 23:00:00"},{"a":"831080","f":"2011-09-23 23:00:00","g":"2011-10-07 23:00:00"}]},{"a":"2959838","b":"ABC National Evening News","e":"news abc4 recent last-chance","f":[{"a":"831928","f":"2011-09-30 23:00:00","g":"2011-10-01 22:00:00"}]}]}
That works just fine.

Oct 12, 2011 at 2:49 AM

OK, thanks for that @VoidMain. You have allowed me to progress and simplify my project :)

The JSON file comes with enclosing [ ] (which JSON.NET doesn't handle, whereas Microsoft does) but I have been stripping them anyway.

But how would you recommend recognizing the multiple arrays / objects (like my example) when I need to consume this and ther JSON formats in the same project? It seems to get back to a lot of regex / string recognition pre-processing, which is why I opted for JSON.NET.

Or is there a simpler way?