JS Parse JSON object detailed description

Source: Internet
Author: User

function Callback (str)

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

...

But now I don't recommend Eval, parse JSON, you can use this

function Jsondecode (data)

{return (The new function ("return" + Data)) ();}


JSON Analysis Instance

JSON is compared to XML, both of which flatten objects (we later explain this "flattened") XML is characterized by a rigorous structure, while JSON is characterized by simple readability and easy to use program analysis because it can easily convert an object into a character stream. For example, the following code:

{' type ': ' Human ', ' name ': ' Hanguofeng ', ' Age ':}



is a JSON expression, and he saves an object, how do we revert it to an object? Very simple, as follows:


var animal_str = ' {' type ': ' Human ', ' name ': ' Hanguofeng ', ' age ': 22} ';

var animal2=eval (' + animal_str + ');



Note the use of parentheses, PHP tutorial passed the value if not included in parentheses, JS has to deal with ... Once the processing is complete, you can access the array like PHP.


Alert (amimal2[1][' ID '))//So.



We use the web effects of the evaluation function, the JSON expression is calculated, and return the value to get an object, here, I think you will be like me, the JSON format of the creator's thinking to admire it.

Originally talk about serialization, "careless" about JSON, and said so much, oh, digress? No, PHP serialization and JSON are very similar, a PHP serialization expression is as follows:

a:3:{s:4: "type"; S:5: "Human"; s:4: "Name"; s:10: "Hanguofeng"; s:3: "Age"; S:2: ";
"



He looks like the structure is somewhat similar to the JSON, in fact, the expression is the serialized result of the following array:

$animal =

Array

(

"Type" => "human",

"Name" => "Hanguofeng",

"Age" => "20"

);


Look at one more example

var myjsonobject = {"Bindings": [
{"Ircevent": "Privmsg", "Method": "Newuri", "regex": "^http://.*"},
{"Ircevent": "Privmsg", "Method": "Deleteuri", "regex": "^delete.*"},
{"Ircevent": "Privmsg", "Method": "Randomuri", "regex": "^random.*"}
]
};myjsonobject.bindings[0].method//"Newuri"

To convert a JSON string into a JSON object, you can use the Eval () function, the eval () function is fast, but because it can compile and parse any JS program, it is not very secure, and if you require security, you can use JSON's parser.

For the Eval () function, the following methods are used:

var myobject = eval (' (' + myjsontext + ') ');

For the use of JSON Paser, the following are:

var myobject = Json.parse (Myjsontext, reviver);

Where Reviver is an optional parameter.

You can also use JSON stringifier to convert a JSON object into a JSON string using the following methods:

var myjsontext = json.stringify (MyObject, replacer);


Where Replacer is an optional parameter function. It can also be used with the following usage:

var Name=document.getelementbyid (' username '); var json={name:name,pass:pass}

Where the fields in JSON are variables. You can use Stringify to convert JSON to strings, using the following: Var jsonstring=json.stringify (JSON);

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.