How to convert a JSON string to an object?

How to convert a JSON string to an object in JavaScript? Is there a way that does this?

Something like:

var x = "{ id: 5, name: 'hello' }"; var y = /*something*/(x); alert(y.id + " " + y.name); 
+7
json javascript jquery
source share
6 answers

According to the comments and the background of the question, it looks like you are already using jQuery. In this case, it’s good to know that jQuery comes with the new parseJSON() function since version 1.4.1 , which was released at the end of January this year. Consider updating if you are not already 1.4.1. Here's an excerpt related to the API documentation:

Description : accepts a valid JSON string and returns the resulting JavaScript object.

Added jQuery.parseJSON (json) : 1.4.1

json JSON string for parsing.

An invalid JSON string will throw an exception. For example, all invalid JSON strings:

  • {test: 1} (it does not have double quotes).
  • {'test': 1} ('test' uses single quotes instead of double quotes).

Also, if you don't pass anything, an empty string, null or undefined, 'null' will be returned from parseJSON . If the browser provides its own implementation of JSON.parse , jQuery uses it to parse the string. For more information on the JSON format, see http://json.org/ .

Example:

Parse the JSON string.

 var obj = jQuery.parseJSON('{"name":"John"}'); alert( obj.name === "John" ); 
+9
source share
+5
source share

Bruno

it uses the jquery method, which, as you will see, uses the same new business ("return ..").

 parseJSON: function (a) { if (typeof a !== "string" || !a) return null; if (/^[\],:{}\s]*$/.test(a.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@") .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]") .replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) return z.JSON && z.JSON.parse ? z.JSON.parse(a) : (new Function("return " + a))(); else c.error("Invalid JSON: " + a) } 

[edit] The regular expression, of course, "works" with any rogue characters embedded in the jr string. json

spooky tho :)

+2
source share

This section fully discusses the built-in JSON implementations and libraries that use native JSON implementations: http://en.wikipedia.org/wiki/JSON#Native_JSON

Using the native JSON implementation will be significantly faster / safer than using some javascript libraries for the same task. However, if some library claims that it will try to use the built-in implementation when possible, this is an even better choice that uses its own JSON directly (compatibility, etc.).

+1
source share

JSON.org provides the simplest solution:

 var y = eval('(' + x + ')'); 

Additional Information

Edit: Oh. Correctly. The eval solution is good if and only if you are sure you can trust the source to create the correct JSON objects. Otherwise, you have to use the JSON parser - look at the other answers.

0
source share

you can also do the following, which is weaker than less evil than eval :):

 var x = '{ "id": 5, "name": "hello" }'; var y = new Function("return " + x)(); alert(y.id + " " + y.name); 

tho, as others say, if you use jquery, go to the parseJson built-in method.

0
source share

All Articles