【问题标题】:WCF response is invalid due to the unexpected string (WCF + JSON)由于意外字符串(WCF + JSON),WCF 响应无效
【发布时间】:2011-03-18 16:07:42
【问题描述】:

我已将 WCF 服务配置为使用 JSON。 我有一个问题,所有条目都被转义了。

    [
   {
      "rel":"http:\/\/localhost:3354\/customer\/1\/order",
      "uri":"http:\/\/localhost:3354\/customer\/1\/order\/3"
   },
   {
      "rel":"http:\/\/localhost:3354\/customer\/1\/order",
      "uri":"http:\/\/localhost:3354\/customer\/1\/order\/5"
   },
   {
      "rel":"http:\/\/localhost:3354\/customer\/1\/order",
      "uri":"http:\/\/localhost:3354\/customer\/1\/order\/8"
   }
]

我确定该消息是有效的,默认不需要转义,是否可以关闭某些字段的转义?

【问题讨论】:

  • 您返回的是:'http:\\localhost:3354\customer\1\order' 还是'localhost:3354/customer/1/order'?
  • 对不起,这里的描述有误。 Wcf 返回 'localhost:3354/customer/1/order';

标签: c# .net wcf json


【解决方案1】:

JSON spec 转义斜线。

字符 任何 Unicode 字符- 除了-”-或--或- 控制字符 \" \ \/ \b \F \n \r \t \u 四位十六进制数

所以:C# 中的“/”在 JSON 中变成了 '\/'

【讨论】:

    【解决方案2】:

    '\' 是JSON 中的控制字符。为了在字符串中包含 \,您必须使用 / 字符对其进行转义。任何 JSON 客户端都应该能够正确解释转义字符,因此您不需要(而绝对不能)对 WCF 代码进行任何修改。

    此外,Pradeep 是对的。 URL 使用正斜杠。

    【讨论】:

    • 是的。这是正确的。 Iphone 开发人员设法解析了这个字符串。这样问题就解决了。
    【解决方案3】:

    http:\localhost:3354\customer\1\order

    为什么要使用反斜杠。常用的方式是有正斜杠。我希望将您的 URL 设为“http://localhost:3354/customer/1/order”可以解决问题。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    相关资源
    最近更新 更多