0

I receive a from the server when loading a page, and it gets populated in the page using Handelbars.java. Till now everything's fine.

However I would like to get that object and store it on a object this is where I fail.

When I try to store it on a object I do the following :

var jsObject = "{{output.items}}";  // the output.items is the JSON retrieved on the template

I get the following (&quote; and line breaks interpreted) :

{
    "profile": {
        "name": "copernic",
        "email": "copernic@cop.com"
    }
    ....
}

When I should get the following (without line breaks interpreted) :

{
    "profile": {
        "name": "copernic",
        "email": "copernic@cop.com"
    }
    ....
}

So it throws me an error on Uncaught SyntaxError: Unexpected token ILLEGAL.

When printing the json on the HTML template using <pre>{{output}}</pre> it looks just fine.

Do you have any idea about how can I store the returned from the server on page loading on a object as I don't have much control of it since it's NOT coming with ?

Thank you.

  • "JSON object" is a misnomer... on the other hand, in what form is the JSON passed from the server to your page then? You might have to parse it, i.e. `JSON.parse()`, but then again it depends on what do you mean by "JSON returned from the server". – Terry Aug 29 '15 at 15:39
  • Neither of the above is valid JSON. Nor valid JS object initializers. And why exactly do you HTML-escape your data? – Oriol Aug 29 '15 at 15:43
  • @Terry : Thanks for your reply. I have a "dynamic" webpage, with Handlebars.java as templating engine. So basically I have a Json object (on a server side term) on my template. I can easily get its elements by for example : `

    {{profile.name}}

    ` or again in JS : `var name = "{{profile.name}}"`. But the issue is when I want to assign the whole object to a JS variable
    –  Aug 29 '15 at 15:47
  • @Oriol : forgot to remove the semicolon and adding the `var` when writing the question :) –  Aug 29 '15 at 15:48
  • @Oriol : I don't escape them, that's the point, it's done automatically when trying to assign the data to the JS object. –  Aug 29 '15 at 15:49
  • 2
    @Copernic I meant that in JSON, property names must be quoted (in JS it's not necessary). And since the values are strings, they must be quoted too. And if you didn't escape them intentionally, you should find what escapes them and disable it, instead of attempting to unescape. – Oriol Aug 29 '15 at 15:49
  • @Oriol : Thank you. They are quoted, I can see that on the server log and when printing the json on a `
    {{output}}
    ` tag. But for some reason the quotes are being escaped
    –  Aug 29 '15 at 15:52

2 Answers2

0

Your server is serving a incorrect json format.

Your server needs to serve a json string, with double quotes.

{
    "profile": {
        "name": "copernic",
        "email": "copernic@cop.com"
    }
}

If you need to serve a well-formed json, you can try with Gson. https://sites.google.com/site/gson/gson-user-guide#TOC-Using-Gson

0

Since the content output.items ist a string, this is how you can proceed.

eval("jsObject={" + object.items.replace(/&quot;/g,'"')+"}");
  • Ok, its not the most elegant one, but it works. SInce the server sends you the correct data you might have a look at the tool/procedure that handles the AJAX request, you might check on what you wanted the ajax routine to give you back. Could it be you requested HTML instead of JASON? – C. van Dorsten Apr 10 '18 at 20:19