Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to escape double quotes between JS and JSON
    primarykey
    data
    text
    <p>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:</p> <blockquote> <p>["PNG","350x150","127 KB"]</p> </blockquote> <p>Where <code>PNG</code> correspond to a particular image's type, where <code>350x150</code> is the image's dimensions and where <code>127 KB</code> is the image's size. Each of these threee values are string variables:</p> <pre><code>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)); </code></pre> <p>The first alert (on the <code>imgDescription</code> variable) prints:</p> <blockquote> <p>["PNG","350x150","127 KB"]</p> </blockquote> <p>So far, so good. However, the minute we pass it to the <code>iVO</code> construct and stringify the resultant JSON, it generates the following output (after I pretty print format it):</p> <pre><code>{ "images": { "4490i45"": { "fizz":"buzz", "imgDesc":"[\"PNG\",\"350x150\",\"127 KB\"]", "foo":"bar" } } } </code></pre> <p>All of my double quotes (") have been escaped (\")!!! Also, the value for <code>imgDesc</code> is enclosed in double-quotes, which is not what we want (see desired JSON below):</p> <p>When I send this JSON back to the server its causing the server to choke.</p> <p>Not sure what is going on here but I've tried several other suggestions, including replacing my double-quotes with <code>'\x22'</code> instances which didn't help.</p> <p>Any ideas as to what would fix this to get the desired result from <code>JSON.stringify(iVO)</code>? Ultimately that's the only thing that matters, that the we end up sending the following to the server:</p> <pre><code>{ "images": { "4490i45"": { "fizz":"buzz", "imgDesc":["PNG","350x150","127 KB"], "foo":"bar" } } } </code></pre> <p>No escaped double-quotes, and the value for <code>imgDesc</code> is not double-quoted. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload