How to convert a JSONString to a Javascript object

I need help with JavaScript. I make an ajax call with a button click that returns the line below, and this is generated using GSON (basically this 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 will I get this data in my javascript. I am trying to convert an object to JSON using the statement below

var jsonObj = eval(xmlHttp.responseText);

xmlHttp.responseText has a line below

{"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 jsp, it stops at var jsonObj = eval(xmlHttp.responseText);

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

If I do not receive this JSON object, I cannot do anything with it. Any help on this could be appreciated.

Regards, Senny

+2
source share
2 answers

JSON, JSON.parse(xmlHttp.responseText), (IE8 +, Firefox, Chrome, Opera, Safari...) eval('(' + xmlHttp.responseText + ')') IE6-7, , (( )) eval:

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

Source: https://habr.com/ru/post/1768290/


All Articles