3

I am using the DataContractJsonSerializer on Windows Phone 7.1 (Mango RC) to pull data from a web service. The data from my web service looks like this:

[
  {
    "Color":"\"black\"",
    "CurrentPlayerTurn":1,
    "GameId":"\"3adbffa7b5744634aca0e4b743014247\"",
    "GameState":0,
    "OtherPlayerId":null
  },
  {
    "Color":"\"black\"",
    "CurrentPlayerTurn":1,
    "GameId":"\"a292247719e34811a93598d2ff3eb13c\"",
    "GameState":0,
    "OtherPlayerId":"\"shmoebob\""
  }
]

In case you're wondering, this data is downstream of a CouchDB map/reduce query, whose output looks like this:

{"total_rows":4,"offset":1,"rows":[
{"id":"3adbffa7b5744634aca0e4b743014247","key":"kotancode","value":[0,1,"black",null]},
{"id":"a292247719e34811a93598d2ff3eb13c","key":"kotancode","value":[0,1,"black","shmoebob"]}
]}

What's happening in my WP7.1 client is that when I deserialize the object array from the first blob of JSON, I am actually getting the quotes inside the strings and I'm having to manually strip them out property by property.

The web service that my WP7.1 client is hitting is a v0.5 WCF Web API RESTful service and I am exposing that data as JSON.

Is there something I'm doing wrong somewhere in this pipeline that is causing the quotes to be treated literally ... or is there something I can do with the DataContractJsonSerializer to make it not actually give me the quotes?

Kevin Hoffman
  • 5,154
  • 4
  • 31
  • 33

1 Answers1

4

This happens to me all the time.. as soon as I post the question, I figure out the answer. The problem was in how I was using JsonValue to parse the information from CouchDB.

the WRONG way:

string color = (row["value"] as JsonArray)[2].ToString(); // this embeds double-quotes

the RIGHT way:

string color = (row["value"] as JsonArray)[2].ReadAs<String>(); // this doesn't embed double-quotes.

Hope this helps someone else who might run into the same issue...

Kevin Hoffman
  • 5,154
  • 4
  • 31
  • 33