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:
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:
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.