JS jquery foreground parsing JSON string

Source: Internet
Author: User


One, JS parsing json string


Eval () and Json.parse ()


For a JSON string returned by the server, if the jquery asynchronous request does not make a type description, or is accepted as a string, an object processing is required, which is to place the string in eval () once. This is also a good way to get JSON objects in a normal Javascipt way, as illustrated below:

var dataobj=eval ("(" +data+ ")");//Convert to JSON object

Why does eval Add "(" +data+ ") here?

The reason is that eval itself is a problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.

The purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement). For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. So the following two execution results are different:

Alert (eval ("{}")); Undefined
Alert (eval ("({})"));//[Object Object]

var data= "{root:[{name: ' 1 ', Value: ' 0 '},{name: ' 6101 ', Value: ' Beijing '},{name: ' 6102 ', Value: ' Tianjin '}]}"; alert (typeof data); var obj = eval ("+data+"), alert (obj.root[1].value);//BEIJING var data1= ' {"root": [{"Name": "1", "value": "0"},{"name": " 6101 "," value ":" Beijing "},{" name ":" 6102 "," Value ":" Tianjin "}]} '; var obj1 = Json.parse (data1); alert (obj1.root[2].value);// Tianjin Alert (eval ("{}")); Undefinedalert (eval ("({})"));//[object Object]

The format requirements of the Json.parse () JSON string are very strict, and the attribute names must be enclosed in double quotation marks and cannot be quoted in single quotes.


Ii. jquery parsing JSON strings



Set datatype as JSON on line, $.get () can write $.get (URL, [data], [callback], "JSON")

Or

$.parsejson (String) can also return an object


<script src= "Js/jquery-2.1.1.min.js" ></script> var obj2= $.parsejson (data1); alert (obj2.root[1].name);


$.parsejson and Json.parse as JSON strings are very strict


This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1731564

JS jquery foreground parsing JSON string

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.