DataContractJsonSerializer 在字符串中留下引号

发布于 2024-12-06 06:56:57 字数 1022 浏览 0 评论 0原文

我正在 Windows Phone 7.1 (Mango RC) 上使用 DataContractJsonSerializer 从 Web 服务中提取数据。来自我的 Web 服务的数据如下所示:

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

如果您想知道,此数据位于 CouchDB Map/Reduce 查询的下游,其输出如下所示:

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

我的 WP7.1 客户端中发生的情况是,当我反序列化对象时从 JSON 的第一个 blob 中获取数组,我实际上是在字符串内获取引号,并且我必须手动将它们逐个属性地剥离出来。

我的 WP7.1 客户端访问的 Web 服务是 v0.5 WCF Web API RESTful 服务,我将该数据公开为 JSON。

我在这个管道中的某个地方做错了什么,导致引号被按字面意思处理……或者我可以用 DataContractJsonSerializer 做些什么,让它实际上不给我引号?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-12-13 06:56:57

这种情况经常发生在我身上..一旦我发布问题,我就会找到答案。问题在于我如何使用 JsonValue 解析来自 CouchDB 的信息。

错误的方法:

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

正确的方法:

string color = (row["value"] as JsonArray)[2].ReadAs(); // 这不嵌入双引号。

希望这可以帮助其他可能遇到同样问题的人......

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文