How JS reads and traverses JSON

Source: Internet
Author: User
Tags eval serialization

Json. The essence is the key-value pair. is the serialization of the Web Effects object, a string that is an array. "JS, strings are objects, arrays are objects, classes are objects, everything is Object"

JSON (JavaScript Object notation) is a lightweight data interchange format. Easy for people to read and write, but also easy to machine parsing and generation. It is based on a subset of JavaScript (standard ecma-262 3rd edition-december 1999). JSON uses a completely language-independent text format, but it also uses a family of C-language (c, C + +, C #, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language

<script>
function A () {
var str = "{" Error ":" Error Here!hello! "}";
var jsonobject = eval (' (' +str+ ') ');
alert (jsonobject.error);
}
</script>
<a onclick= "A ();" >test</a>

Let's look at a simple example

{date:1999-9-9,x:100,y:100}, [[' John ', ' 20 '],[' Dick ', ' 30 ']]; This is JSON. The essence is the key-value pair. is the serialization of a JavaScript object, a string that is an array. "JS, strings are objects, arrays are objects, classes are objects, everything is Object"

<script language= "JavaScript" >
var menu1 =
{
"A": {title: "1"},//in JS, the key value has no double quotes are indifferent, not wrong, grammar is so stipulated.
b:{"title": "2"},
C:{title: "3"},
D:{title:4}
}
alert (menu1.a.title);
for (var x in menu1)
{
document.write (x.tostring () + "</br>");
document.write (menu1[x].title+ "</br>");
document.write ("</br>==================</br>");
}
</script>

Okay, now it's time to provide a website.

String.prototype.parsejson = function () {try {return! /[^,:{}[]0-9.-+eaeflnr-u nrt]/.test (This.replace (/) (. | [^"]) *?"    /g, ')) && eval (' (' + this + ') ');    catch (e) {return false; }};

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.