Example of using the client to call JSON with JavaScript

Source: Internet
Author: User

I have already introduced what is JSON. Next I will use examples to learn how to use JavaScript to call JSON on the client.

First, an open-source JSON parser and string converter provided on the official JSON Website: JSON. js.

According to the JSON. js explanatory document, you can know:

Array. tojsonstring (whitelist)
Boolean. tojsonstring ()
Date. tojsonstring ()
Number. tojsonstring ()
Object. tojsonstring (whitelist)
String. tojsonstring ()

The preceding six functions can generate JSON text without any periodic parameters. invalid values are excluded. the default conversion of date is an ISO string. you can add the tojsonstring () function to any date object for different rendering. the object and array functions include the whitelist Parameter options. whitelist is a string array. If it is provided, keys not found in objects in whitelist will be excluded.

String. the parsejson (filter) function interprets JSON text to generate objects and arrays. It can throw syntax errors. the filter parameter can be used to filter and convert the results. It can accept each key and value. It can return the used value instead of the original value. if it returns the received message, the structure will be modified. If undefined (undefined) is returned, the member will be deleted.

Example:

// Interpreted text. If the key contains a string of 'date', convert the value to date.

Mydata = text. parsejson (function (Key, value ){
Return key. indexof ('date')> = 0? New Date (value): value;
});

The following is an example of converting an array into a corresponding JSON string:

// Contains JSON. js in advance
Function tojsonexample (){
VaR jnbtn = Document. getelementbyid ("JN ");
Jnbtn. onclick = function (){
VaR continents = new array ();
Continents. Push ("Europe"); // add elements to the array
Continents. Push ("Asia ");
Continents. Push ("Australia ");
Continents. Push ("Antarctica ");
Continents. Push ("North America ");
Continents. Push ("South America ");
Continents. Push ("Africa ");
Alert ("The JSON representation of the continents array is:" +
Continents. tojsonstring ());
}
 
 
}

Result:

["Europe", "Asia", "Australia", "Antarctica", "North America", "South America ", "Africa"] The following is a JSON string converted into an array. Here an eval function is required to explain the JSON string. The eval function is used to receive a validation Javascript Code The following code often needs to convert the JSON text to the original form. VaR value = eval ("(" + jsontext + ")"); Example: function topasejsonexample (){
VaR jnbtn = Document. getelementbyid ("JN ");
Jnbtn. onclick = function (){
VaR arrayasjsontext = '["Europe", "Asia", "Australia", "Antarctica", "North America", "South America", "Africa"]';
VaR continents = eval (arrayasjsontext); // verify the arrayasjsontext Array
VaR continents = arrayasjsontext. parsejson ();
Document. getelementbyid ("jsonrespose"). innerhtml = continents;
}
} Result: Europe, Asia, Australia, Antarctica, North America, South America, and Africa Summary: here, I just briefly introduced the application of JSON on the client, tojsonstring () and how to use parsejson. for the server side (Asp, Asp. net.
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.