I got some hints/reminders on how to Consume a JSON object in Silverlight, and found Jsonlint really helpful in tuning the output until it was valid Json.
![](http://2.bp.blogspot.com/_Ba76y6K7kvs/SgVlFs9sDrI/AAAAAAAAA5M/jWSY_joQIsg/s320/JsonLint.png)
Then I created a 'matching' object model in Silverlight C#
![](http://3.bp.blogspot.com/_Ba76y6K7kvs/SgVlF384BTI/AAAAAAAAA5U/aopLdv8f13Y/s320/ObjectModel.png)
and wired up the
OpenReadCompleted
event using DataContractJsonSerializer
![](http://4.bp.blogspot.com/_Ba76y6K7kvs/SgVlJCgQjpI/AAAAAAAAA5c/b4LYNlJRLeY/s400/Code.png)
Everything LOOKED like it should work, so at first I was confused by this error message:
Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]'
After staring blankly at the code for a while, I finally realised where my Json had gone wrong -- at the very root of my Json response (see Jsonlint image above) I was "accidentally" wrapping the entire Json output with an unnecessary [] pair. I guess this meant the Deserializer was expecting to cast into a collection (albeit with a single element), but I was intending the root Json element to be a single object (to match my C#
JsonCourse
).The simple fix was removing the enclosing [] so that my Json started off like this instead:
{Lesson for tonight is to better understand the underlying Json representation before consuming it (or else use a library rather than hand-craft the output). Anyway now it is fixed RaceReplay.net 2.0 is that much closer to fruition...
"width": 800 ,
"height": 600 ,
"runners": [
{
"name": "mapcanberramarathon",
"points": [
No comments:
Post a Comment
Note: only a member of this blog may post a comment.