如何将JSON字符串转换为JS对象?

我正在使用JS对象创建带有Google可视化的图形,并尝试设计数据源。首先,我创建了一个JS对象客户端。

var JSONObject = {
  cols: [{
      id: 'date',
      label: 'Date',
      type: 'date'
    },
    {
      id: 'soldpencils',
      label: 'Sold Pencils',
      type: 'number'
    },
    {
      id: 'soldpens',
      label: 'Sold Pens',
      type: 'number'
    }
  ],
  rows: [{
      c: [{
        v: new Date(2008, 1, 1),
        f: '2/1/2008'
      }, {
        v: 30000
      }, {
        v: 40645
      }]
    },
    {
      c: [{
        v: new Date(2008, 1, 2),
        f: '2/2/2008'
      }, {
        v: 14045
      }, {
        v: 20374
      }]
    },
    {
      c: [{
        v: new Date(2008, 1, 3),
        f: '2/3/2008'
      }, {
        v: 55022
      }, {
        v: 50766
      }]
    }
  ]
};

var data = new google.visualization.DataTable(JSONObject, 0.5);

现在我需要动态地获取数据。因此,我向返回JSON字符串的页面发送了如下Ajax请求:

 "cols: [{id: 'date', label: 'Date', type: 'date'},
{id: 'soldpencils', label: 'Sold Pencils', type: 'number'},
{id: 'soldpens', label: 'Sold Pens', type: 'number'}],
  rows: [{c:[{v: new Date(2008,1,1),f:'2/1/2008'},{v: 30000}, {v: 40645}]},
      {c:[{v: new Date(2008,1,2),f:'2/2/2008'},{v: 14045}, {v: 20374}]},
{c:[{v: new Date(2008,1,3),f:'2/3/2008'},{v: 55022}, {v: 50766}]}"

我把它保存到一个变量中:

var var1 = "cols: [{i ....... 66}]}"

显示为:

alert(var1);

现在,我的任务是从该字符串创建一个JS对象——但这是行不通的。 当我使用JS对象时,一切正常,并且能够获得所需的图形。一旦我尝试将从警报消息中确认的AJAX请求中的相同字符串值放入n对象中,则无法正确创建该对象。请帮帮我吧!

You can use eval(jsonString) if you trust the data in the string, otherwise you'll need to parse it properly - check json.org for some code samples.

You can use this library from JSON.org to translate your string into a JSON object.

var var1_obj = JSON.parse(var1);

Or you can use the jquery-json library as well.

var var1_obj = $.toJSON(var1);

Some modern browsers have support for parsing JSON into a native object:

var var1 = '{"cols": [{"i" ....... 66}]}';
var result = JSON.parse(var1);

For the browsers that don't support it, you can download json2.js from json.org for safe parsing of a JSON object. The script will check for native JSON support and if it doesn't exist, provide the JSON global object instead. If the faster, native object is available it will just exit the script leaving it intact. You must, however, provide valid JSON or it will throw an error — you can check the validity of your JSON with http://jslint.com or http://jsonlint.com.

the string in your question is not a valid json string. From json.org website:

JSON is built on two structures:

* A collection of name/value pairs. In various languages, this is 
  realized as an object, record, struct, dictionary, hash table, keyed list, or
  associative array.
* An ordered list of values. In most languages, this is realized as an
  array, vector, list, or sequence.

Basically a json string will always start with either { or [.

Then as @Andy E and @Cryo said you can parse the string with json2.js or some other libraries.

IMHO you should avoid eval because it will any javascript program, so you might incur in security issues.

The string you are returning is not valid JSON. The names in the objects needs to be quoted and the whole string needs to be put in { … } to form an object. JSON also cannot contain something like new Date(). JSON is just a small subset of JavaScript that has only strings, numbers, objects, arrays, true, false and null.

See the JSON grammar for more information.