How do JSON strings translate into objects?

Source: Internet
Author: User

  Analytical

  1, definition: Refers to the process of converting strings that conform to JSON syntax rules into objects.

  2, different programming languages provide a Way to parse JSON strings, here is the main explanation in JavaScript parsing methods. There are three main types:

  1)- use eval ()

  2)- use json.parse ()

  3)- use third-party libraries, such as JQuery , etc.

  Eval ()

  1. Theeval () function parameter is a string whose function is to execute the JavaScript code directly within it .

  2.eval () can parse the JSON string. It can also be seen from here thatJSON and JavaScript are highly chimeric.

  3, however, it is seldom used directly to parse the eval () , if your browser version is really very old, you may need this method. In addition,eval () is a relatively dangerous function because the string may contain unknown elements. Here, as a study, still need to know that this is also a method.

  4, please pay attention to the parameters of the eval () , the string is surrounded by parentheses, which is necessary, otherwise it will be an error.

  5. Because the JSON string is surrounded by curly braces ("{}"), putting it directly to eval () is executed as a block of statements. Therefore, enclose the brackets on both sides to make them into an expression.

  Sample code

<script>

var str = "console.log ('hello')" ;

Eval (SRE);

</script>

Console output Execution Results:

  Sample code

<script>

var str = ' { " name " : " geoff Lui " " age "&NBSP; : " ;   // conforms to json Span style= "font-family: Arial" > syntax rules for strings

Eval ("("+str+")");

Console.log (obj);

</script>

Execution Result:

  Json.parse ()

  1, most browsers now support json.parse (), is recommended to use the way

Example code:

<script>

var str = '{ 'name' : 'Geoff Lui' , " Age " : +-} '   ; strings that conform to JSON Syntax rules

var obj = json.parse (str);

Console.log (obj);

</script>

Execution Result:

  2. If you enter a string that does not conform to the specification, the error will be

  3,json.parse () can have a second parameter, is a function. This function has two parameters: name and value, respectively, representing names and values. When a json stringis passed in, each set of JSON name / value pairs calls this function. The function has a return value, and the return value is assigned to the current name.

Example code:

<script>

var str = '{ 'name' : 'Geoff Lui' , " Age " : +-} '   ; strings that conform to JSON Syntax rules

var obj = json.parse (str,fun);

function fun (name, value) {

Console.log (name + ": " + value);

return value;

}

Console.log (obj);

</script>

Execution Result:

Example code:

<script>

var str = '{ 'name' : 'Geoff Lui' , " Age " : +-} '   ; strings that conform to JSON Syntax rules

var obj = json.parse (str,fun);

function fun (name, value) {

if (name = = "Age" )

value = 14;

return value;

}

Console.log (obj);

</script>

Execution Result:

4, using the second parameter, you can parse the JSON string while processing some of the data.

original link:http://www.maiziedu.com/wiki/json/conversion/

How do JSON strings translate into objects?

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.