Method to convert a string to a json object
- 2020-03-30 00:01:46
- OfStack
JSON is a lightweight data interchange format that is easy to manipulate. Easy to read and write. It is also easy for machines to parse and generate. Please refer to: (link: http://www.json.org/).
Many times we need to assemble a string as a json object, first by combining the string and then converting it to a json object, as shown in the following example:
<script type="text/javascript">
<!--
var a=50,b="xxx";
var arr="{id:"+a+",name:'"+b+"'}";
//-->
</script>
The next step is to convert it to an object. We'll soon think of using the eval method, but if that turns out to be a mistake, and I've tried it today, how do we convert it to a json object? After a long time of frustration, I found a solution in the json.js file provided by the official website of json. The solution is as follows: