Home  >  Article  >  Web Front-end  >  Methods to convert Json and string to each other

Methods to convert Json and string to each other

php中世界最好的语言
php中世界最好的语言Original
2018-03-19 17:14:481639browse

This time I will bring you the method of converting Json and string to each other. What are the notes for converting Json and string to each other? The following is a practical case. Let’s take a look. one time.

jQuery Conversion methods supported by the plug-in:

$.parseJSON(jsonstr); //jQuery.parseJSON(jsonstr), you can convert json StringConvert to json object

Conversion methods supported by browsers (Firefox, chrome, opera, safari, ie9, ie8) and other browsers:

JSON.parse(jsonstr); //You can convert json string into json object

JSON.stringify(jsonobj); //You can convert json object into json For strings

Note: ie8 (compatibility mode), ie7 and ie6 do not have JSON objects. It is recommended to use the official JSON method and introduce json.js.

JavascriptSupported conversion methods:

eval_r('(' + jsonstr + ')'); //You can convert json string Convert to a json object. Note that you need to wrap a pair of parentheses around the json character

Note: ie8 (compatibility mode), ie7 and ie6 can also use eval_r() to convert the string into a JSON object, but it is not recommended. These methods, this method is unsafe, eval will execute the expression in the json string.

JSON official conversion method:

http://www.json.org/ provides a json.js, such as ie8 (compatibility mode), ie7 and IE6 can support JSON objects and their stringify() and parse() methods;

You can get this js at https://github.com/douglascrockford/JSON-js, Generally, json2.js is used now.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

What class definition components are there in React

How to extract numbers from a string with regular expressions

The above is the detailed content of Methods to convert Json and string to each other. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn