English 中文(简体)
如何避免在联合材料和JSON之间重复引用
原标题:How to escape double quotes between JS and JSON

我试图在联合材料中构筑一条能够作为非常特殊形式传入“智者”。 预期成果如下:

[“PNG”,“350x150”,“127 KB”

对应特定图像类型:350x150<>>>> 代码为图像尺寸,127 KB为图像尺寸。 这三个数值中每个数值都包含以下变量:

var imgType = getImageType(); // Returns "PNG"
var imgDim = getImageDim(); // Returns "350x150"
var imgSize = getImageSize(); // Returns "127 KB"

var imgDescription =  ["  + imgType +  ","  + imgDim +  ","  + imgSize +  "] ;

// Sanity check
alert(imgDescription);

iVO.images[thisImage] = {
    "fizz":"buzz",
    "imgDesc":imgDescription,
    "foo":"bar"
}

alert(JSON.stringify(iVO));

第一份警报(关于<代码>imgDescriptionps>):

[“PNG”,“350x150”,“127 KB”

迄今为止,情况良好。 然而,我们通过<代码>iVO建造并强化由此产生的JSON,生成以下产出(在印本之后):

{
    "images":
    {
        "4490i45"":
        {
            "fizz":"buzz",
            "imgDesc":"[“PNG”,“350x150”,“127 KB”",
            "foo":"bar"
        }
    }
}

我的两句“!”! 此外,<代码>imgDesc的价值载于双重报价中,这并不是我们想要的(见下文所希望的JSON):

当我把这名JSON送回服务器时,它使服务器cho。

我不敢肯定这里正在做些什么,但我曾尝试过其他几项建议,包括用<代码>×22取代我双重引用的事例。

关于如何从<条码>中取得预期结果的想法? 归根结底,这是我们最终向服务器派遣以下人员的唯一事情:

{
    "images":
    {
        "4490i45"":
        {
            "fizz":"buzz",
            "imgDesc":[“PNG”,“350x150”,“127 KB”,
            "foo":"bar"
        }
    }
}

没有人逃脱双重报价,<代码>imgDesc的价值没有双重引用。 提前感谢!

最佳回答

为什么你只是把守规作为常规阵列

var imgDescription = [imgType , imgDim, imgSize];

Stringify should take care of what you are trying to do, otherwise you are passing imgDescription as a string and stringify would escape the quotes.

e.g.

var imgType = "PNG";
var imgDim = "350x150";
var imgSize = "127 KB";
var d = {
    "fizz":"buzz",
    "imgDesc":[imgType , imgDim, imgSize],
    "foo":"bar"
}
console.log(JSON.stringify(d));

产出:

{"fizz":"buzz","imgDesc":["PNG","350x150","127 KB"],"foo":"bar"}
问题回答

暂无回答




相关问题
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.

热门标签