Json string, json object, array conversion, json string
- Converts a json string to a json object.
// Jquery method var jsonObj = $. parseJSON (jsonStr) // js method var jsonObj = JSON. parse (jsonStr)
- Converts a json object to a json string.
// Js method var jsonStr1 = JSON. stringify (jsonObj)
Conversion between json objects and json strings
Example 1: If I have two variables, I want to convert a to a string and convert B to a JSON object:
Var a = {"name": "tom", "sex": "male", "age": "24"}; var B = {"name ": "Mike", "sex": "female", "age": "29 "};
Advanced browsers such as Firefox, chrome, opera, safari, ie9, and ie8 can directly use the stringify () and parse () Methods of JSON objects.
JSON. stringify (obj) converts JSON into a string. JSON. parse (string) converts a string to JSON format;
The preceding conversions can be written as follows:
Var a = {"name": "tom", "sex": "male", "age": "24"}; var B = {"name ": "Mike", "sex": "female", "age": "29"}; var aToStr = JSON. stringify (a); var bToObj = JSON. parse (B); alert (typeof (aToStr); // stringalert (typeof (bToObj); // object
JSON. stringify ()
Ie8 (compatible mode), ie7 and ie6 do not have JSON objects, but http://www.json.org/with A json.js, this example of ie8 (compatible mode), ie7 and ie6 can support JSON objects and their stringify () and parse () methods;
You can.
Ie8 (compatible mode), ie7 and ie6 can use eval () to convert strings into JSON objects,
Var c = {"name": "Mike", "sex": "female", "age": "29 "}; var cToObj = eval ("(" + c + ")"); alert (typeof (cToObj ));
JQuery also provides the JSON format conversion method jQuery. parseJSON (json). It accepts a standard JSON string and returns the parsed JavaScript (JSON) object.
If you are interested, you can encapsulate a jQuery extension. jQuery. stringifyJSON (obj) converts JSON into a string.
Example 2:
<Script type = "text/javascript"> var jsonStr = '[{"id": "01", "open": false, "pId": "0 ", "name": "department A" },{ "id": "01", "open": false, "pId": "0", "name ": "Department A" },{ "id": "011", "open": false, "pId": "01", "name": "department "},
{"Id": "03", "open": false, "pId": "0", "name": "department A" },{ "id ": "04", "open": false, "pId": "0", "name": "department A" },{ "id": "05 ", "open": false, "pId": "0", "name": "department A" },{ "id": "06", "open": false, "pId": "0", "name": "department A"}] '; // var jsonObj = $. parseJSON (jsonStr); var jsonObj = JSON. parse (jsonStr) console. log (jsonObj) var jsonStr1 = JSON. stringify (jsonObj) console. log (jsonStr1 + "jsonStr1") </script>
- Converts a json object to an array.
<Script type = "text/javascript"> var jsonStr = '[{"id": "01", "open": false, "pId": "0 ", "name": "department A" },{ "id": "01", "open": false, "pId": "0", "name ": "Department A" },{ "id": "011", "open": false, "pId": "01", "name": "department "},
{"Id": "03", "open": false, "pId": "0", "name": "department A" },{ "id ": "04", "open": false, "pId": "0", "name": "department A" },{ "id": "05 ", "open": false, "pId": "0", "name": "department A" },{ "id": "06", "open": false, "pId": "0", "name": "department A"}] '; // var jsonObj = $. parseJSON (jsonStr); var jsonObj = JSON. parse (jsonStr) console. log (jsonObj) var jsonStr1 = JSON. stringify (jsonObj) console. log (jsonStr1 + "jsonStr1") var jsonArr = []; for (var I = 0; I <jsonObj. length; I ++) {jsonArr [I] = jsonObj [I];} console. log (typeof (jsonArr) </script>