Home Web Front-end JS Tutorial Detailed explanation of the method of converting string to json object_Basic knowledge

Detailed explanation of the method of converting string to json object_Basic knowledge

May 16, 2016 pm 05:11 PM
json object string

JSON is a lightweight data exchange format that is easy to operate and use. Easy for humans to read and write. It is also easy for machines to parse and generate. For detailed introduction, please see: http://www.json.org/.

Many times we need to assemble strings into json objects. We must first assemble the strings and then convert them into json objects, as in the following example:

Copy code The code is as follows:

<script type="text/javascript">
<!--
var a=50,b=" xxx";
var arr="{id:" a ",name:'" b "'}";
//-->
</script>

is combined into a string arr. The next step is to convert it into an object. Soon we will think of using the eval method, but if we do this, an error will occur in the conversion. I tried this today. How to convert it into a json object? Woolen cloth? After being depressed for a long time, I found a solution in the json.js file provided by the json official website. The method is as follows:

Add parentheses at both ends of the string and then eval will be ok. The test code is as follows:

Copy code The code is as follows:

<script type="text/ javascript">
<!--
var a=50,b="xxx";
var arr="{id:" a ",name:'" b "'}";
arr=eval('(' arr ')')
alert(arr.name);
//-->
</script>

After the above code is executed, "xxx" will pop up, indicating that it has been successfully converted into a json object. It seems to be a very simple problem, but it still took me a long time to solve it. I still wrote it on the blog to deepen my impression. I also hope it can help others. Friends who have encountered this problem can get rid of their depression as soon as possible.
Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

How to determine whether a Golang string ends with a specified character

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang?

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

How to repeat a string in python_python repeating string tutorial

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

PHP techniques for deleting the last two characters of a string

See all articles