Multiple methods for converting strings to Json

Source: Internet
Author: User

JSON is a lightweight data exchange format for ease of operation. Easy to read and write. It is also easy to parse and generate machines.

Most of the time, we need to assemble a string as a json object. First, we need to combine the string and then convert it into a json object. The following example shows how:

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

Combined into the string arr, the next step is to convert it into an object, and soon we will think of using the eval method, but if this conversion will produce an error, today I will try it like this, how can we convert it into a json object? After being depressed for a long time, I found a solution in the json. js file provided on the json official website. The solution is as follows:

Add parentheses at both ends of the string and then the eval will be OK. The test 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 to a json object. It seems a very simple problem, but it still takes a long time to solve it, I still remember my blog to get a better impression and hope to help my friends who have encountered this problem relieve their worries as soon as possible.

The following is a detailed example:

 

In this example, json.txt is:

{"user":"cck","sex":"name"}

During ajax development projects, strings in json format are often returned to the front end, and the front end is parsed into js objects (JSON ). The JSON concept in ECMA-262 (E3) was not written to the standard, and the JSON concept in ECMA-262 (E5) was formally introduced, including the Global JSON object and the toJSON method of Date.

1. eval parsing, I'm afraid this is the earliest parsing method. As follows:

function strToJson(str){     var json = eval('(' + str + ')');     return json;}

Remember the parentheses on both sides of str.

2. The new Function format is weird. As follows:

function strToJson(str){    var json = (new Function("return " + str))();    return json;}

3. Use the global JSON object as follows:

function strToJson(str){    return JSON.parse(str);}

Currently, IE8/Firefox3.5 +/Chrome4/Safari4/Opera10 has implemented this method.

Sometimes the data read from the database contains spaces to invalidate eval. In this case, remove the spaces in the string.

var result = xmlhttp.responseText;text = result.replace(/\s/ig, ' ');var json = eval('(' + text + ')');gen3_title.innerHTML = json.title;gen3.innerHTML = json.content;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.