English 中文(简体)
在把JSON混为一谈时,造成这种同义的错误是什么?
原标题:What is causing this syntax error while parsing JSON?

I have a string in javascript:

var test =  {"test":"\-"} 

当我试图将这一点与以下几个方面混为一谈:

JSON.parse(test)

or with:

$.parseJSON(test)

I get a SyntaxError of type "unexpected_token_number". This the value for the "test" attribute is a user enterable field. How should I properly escape this field?

最佳回答

{"test":"\-"} will be interpreted as JavaScript string, with the result being {"test":"-"}.

As you can see in these diagrams, - is not a valid escape sequence (valid are ", \, /, , f, , , , uxxxx).

JSONLint 并给出错误

Parse error on line 2:
{    "test": "-"}
-------------^
Expecting  STRING ,  NUMBER ,  NULL ,  TRUE ,  FALSE ,  { ,  [ 

如果你想在JSON两颗斜线,你也必须在Java的座标中逃脱。

var test =  {"test": "\\-"} ;

否则:

var test =  {"test": "-"} ;
问题回答

阁下:

{
    "test": "-"
}

rel=“nofollow”

你们也许希望有这样的想法:

{
    "test": "\-"
}

并且未能将其转换成一个适当显示的 Java本,但不能放弃“<>条码/代码>的 Java本。

var test =  {"test":"\\-"} 

各位:

var test =  {"test":"-"} 
JSON.parse(test)

你们想要的是吗? 否:<代码>试验:“-”}?

Otherwiste:

var test =  {"test":"\\-"} 
JSON.parse(test)

备有:{test:>->}

Cheers

你们想要检验的价值是什么? 。 假设你再次试图越航,而且你希望测试值成为<条码>-,这将发挥作用:

var test =  {"test":"\\-"} 

//Result: { test="-"}

当我试图平息这种扼杀时,我就“过错”。

原因是,你具有不需要放弃的特性。 The in the stringlich is an out reslash, so the string contained the independence Code>-. 不需要 escaping,因此JSON教区不允许逃跑。

如果你想在“智者”身上 escaped倒,你就不得不躲避,把它放在字面上:

var test =  {"test":"\\-"} 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.