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.