JSON - 如何在JSON中允许特殊字符?

时间:2013-09-18 12:24:44

标签: c# .net json

Response.ContentType = "text/plain";

System.IO.Stream inptStrm = Request.InputStream;

byte[] bytes = new byte[inptStrm.Length];

int i = inptStrm.Read(bytes, 0, Convert.ToInt32(inptStrm.Length));

string Input = Encoding.UTF8.GetString(bytes);
JsonTextParser parsor = new JsonTextParser();

JsonObject jsonObj = parsor.Parse(Input);

我的输入字符串是:::::

{
  "function":"addwhy",

  "lastname":"\"", // this line


}

我的代理也尝试将其转换为UTF8格式,但是,当代码转到游标时,我的代码会生成错误。

3 个答案:

答案 0 :(得分:1)

它应该只是一个反斜杠,如......

"shopname":"\"\""
"lastname": "\"\"",

如果你添加这个JSON ......

{
    "function": "addwhy",
    "firstname": "firstname",
    "lastname": "\"\"",
    "dob": "8/8/2001",
    "dop": "testplace",
    "street": "teststreet",
    "nr": "testnr",
    "postcode": "123456",
    "place": "testplace",
    "telephone": "telephone",
    "incidentid": "1",
    "Aangehoudendoor": "testAangehoudendoor",
    "Waar": "testWaar",
    "DayTime": "Monday,
    6: 48PM",
    "createdby": "1",
    "updatedby": "0",
    "shopId": "1",
    "witneesid": "1",
    "op": "",
    "om": "testom"
}

..到JSONLint validator,您会看到验证成功。

答案 1 :(得分:0)

您的数据出错了。客户端应在创建JSON之前转义引号。

对JavaScript使用JSON.stringify或关注Escape Quotes in Objective-C

答案 2 :(得分:0)

在解析时不应该出错,而应该在响应中处理引号(即避免格式错误的JSON响应)。尝试将引号用作:

  ....
  ....
  "lastname":"""", 
  "dob":"8/8/2001",
  "dop":"testplace",
  ....
  ....