最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to escape double quotes between JS and JSON - Stack Overflow

programmeradmin2浏览0评论

I'm trying to construct a String in JS that can be passed into JSON as an with a very particular format. Desired result is a string of the following form:

["PNG","350x150","127 KB"]

Where PNG correspond to a particular image's type, where 350x150 is the image's dimensions and where 127 KB is the image's size. Each of these threee values are string variables:

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));

The first alert (on the imgDescription variable) prints:

["PNG","350x150","127 KB"]

So far, so good. However, the minute we pass it to the iVO construct and stringify the resultant JSON, it generates the following output (after I pretty print format it):

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

All of my double quotes (") have been escaped (\")!!! Also, the value for imgDesc is enclosed in double-quotes, which is not what we want (see desired JSON below):

When I send this JSON back to the server its causing the server to choke.

Not sure what is going on here but I've tried several other suggestions, including replacing my double-quotes with '\x22' instances which didn't help.

Any ideas as to what would fix this to get the desired result from JSON.stringify(iVO)? Ultimately that's the only thing that matters, that the we end up sending the following to the server:

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

No escaped double-quotes, and the value for imgDesc is not double-quoted. Thanks in advance!

I'm trying to construct a String in JS that can be passed into JSON as an with a very particular format. Desired result is a string of the following form:

["PNG","350x150","127 KB"]

Where PNG correspond to a particular image's type, where 350x150 is the image's dimensions and where 127 KB is the image's size. Each of these threee values are string variables:

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));

The first alert (on the imgDescription variable) prints:

["PNG","350x150","127 KB"]

So far, so good. However, the minute we pass it to the iVO construct and stringify the resultant JSON, it generates the following output (after I pretty print format it):

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

All of my double quotes (") have been escaped (\")!!! Also, the value for imgDesc is enclosed in double-quotes, which is not what we want (see desired JSON below):

When I send this JSON back to the server its causing the server to choke.

Not sure what is going on here but I've tried several other suggestions, including replacing my double-quotes with '\x22' instances which didn't help.

Any ideas as to what would fix this to get the desired result from JSON.stringify(iVO)? Ultimately that's the only thing that matters, that the we end up sending the following to the server:

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

No escaped double-quotes, and the value for imgDesc is not double-quoted. Thanks in advance!

Share Improve this question asked May 2, 2012 at 20:23 IAmYourFajaIAmYourFaja 57k186 gold badges489 silver badges778 bronze badges 5
  • Have you tried adding extra escaped quotes? – dweiss Commented May 2, 2012 at 20:25
  • Sounds like a decent suggestion @dweiss - any chance you can provide a code example? Thanks again! – IAmYourFaja Commented May 2, 2012 at 20:27
  • Your target is not valid JSON ("imgDesc":["PNG","350x150","127 KB"] etc.). – Will Commented May 2, 2012 at 20:28
  • For example, if you tried var imgDescription = '["\"' + imgType + '\"","\"' + imgDim + '\"","\"' + imgSize + '\""]'; – dweiss Commented May 2, 2012 at 20:29
  • Oh, I thought you were TRYING to force quotes. Now I see that you're just trying to get the JSON to parse properly. Anurag's answer probably will suffice, in that case. – dweiss Commented May 2, 2012 at 20:30
Add a ment  | 

1 Answer 1

Reset to default 6

Why don't you just put imgDescription as regular array

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));

Output:

{"fizz":"buzz","imgDesc":["PNG","350x150","127 KB"],"foo":"bar"}
发布评论

评论列表(0)

  1. 暂无评论