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

json - how to convert JSONString to Javascript Object - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Mar 28, 2012 at 9:05 Sirko 74.1k19 gold badges155 silver badges190 bronze badges asked Mar 28, 2012 at 9:02 user57421user57421 7,9517 gold badges27 silver badges22 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

jQuery can do it:

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

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

UPDATE Just use the in-built JSON object

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 + ')');
发布评论

评论列表(0)

  1. 暂无评论