2

I need some help on the JavaScript. I do a ajax call on a button click, which returns the below String and this is generated using GSON(basically it is a JSON object).

{ 
  "chart":{
      "renderTo":"container",
      "type":"bar"
   },
   "title":{
      "text":"Engagement Per Vendor Per GBP"
   },
   "subtitle":{
      "text":"ASPT"
   },
   "xAxis":{
      "categories":[
         "A",
         "B",
         "C",
         "D"
      ],
      "title":{
         "text":"Engagement Per Vendor Per GBP"
      }
   },
   "yAxis":{
      "min":0,
      "title":{
         "text":"Count",
         "align":"high"
      }
   },
   "plotOptions":{
      "bar":{
         "dataLabels":{
            "enabled":true
         }
      }
   },
   "legend":{
      "layout":"vertical",
      "align":"right",
      "verticalAlign":"bottom",
      "x":-100,
      "y":100,
      "floating":true,
      "borderWidth":1,
      "backgroundColor":"#FFFFFF",
      "shadow":true
   },
   "credits":{
      "enabled":true
   },
   "series":[
      {
         "name":"ABC",
         "data":[
            10,
            20,
            20,
            30
         ]
      },
      {
         "name":"DEF",
         "data":[
            10,
            20,
            30,
            40
         ]
      },
      {
         "name":"GHIJ",
         "data":[
            20,
            30,
            40,
            10
         ]
      },
      {
         "name":"KLMN",
         "data":[
            10,
            40,
            20,
            30
         ]
      }
   ]
}

When I get this data in my javascript. I’m trying to convert the object to JSON using the below statement

var jsonObj = eval(xmlHttp.responseText);

xmlHttp.responseText has the below string

{"chart":{"renderTo":"container","type":"bar"},"title":{"text":"Engagement Per Vendor Per GBP"},"subtitle":{"text":"ASPT"},"xAxis":{"categories":["A","B","C","D"],"title":{"text":"Engagement Per Vendor Per GBP"}},"yAxis":{"min":0,"title":{"text":"Count","align":"high"}},"plotOptions":{"bar":{"dataLabels":{"enabled":true}}},"legend":{"layout":"vertical","align":"right","verticalAlign":"bottom","x":-100,"y":100,"floating":true,"borderWidth":1,"backgroundColor":"#FFFFFF","shadow":true},"credits":{"enabled":true},"series":[{"name":"ABC","data":[10,20,20,30]},{"name":"DEF","data":[10,20,30,40]},{"name":"GHIJ","data":[20,30,40,10]},{"name":"KLMN","data":[10,40,20,30]}]}

When I try to run the jsp, it stops at var jsonObj = eval(xmlHttp.responseText);

I have done this before many time, but this time the data is different. The JSON string was created by by a JSON method from GSON api.

Unless I get this in to a JSON object, I’ll not be able to do anything with it. Any help on this is appreaciated.

Regards, Senny

2 Answers 2

3

jQuery can do it:

http://api.jquery.com/jQuery.parseJSON/

If you don't want to use eval, here is a standalone parser: https://github.com/douglascrockford/JSON-js

UPDATE Just use the in-built JSON object

Sign up to request clarification or add additional context in comments.

Comments

3

This is a valid JSON format, so use JSON.parse(xmlHttp.responseText) when possible (IE8+, Firefox, Chrome, Opera, Safari...), and use eval('(' + xmlHttp.responseText + ')') for IE6-7, note you should add an extra pair of bracket (( and )) when using eval:

if (JSON) return JSON.parse(xmlHttp.responseText);
else return eval('(' + xmlHttp.responseText + ')');

2 Comments

+1 for the suggestion of JSON.parse (and I wish I could -1 at the same time, without doing +0, for not suggesting using a library to patch/shim-in JSON.parse on older browsers)
A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.