One, what is JSON
JSON (JavaScript Object notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy to machine parse and generate. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 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.
Second, what is JSONP
JSONP (JSON with Padding) is a "usage pattern" of data format JSON that allows pages to be data from other domains. Another new way to solve this problem is to share across source resources.
JSONP
Definition: is a cross-domain way of accessing data.
Function and principle: After the client declares the callback callback function, the client requests the data across the domain through the script tag, then the server returns the corresponding JSON data and executes the callback function dynamically.
The following are used in jquery:
JQuery JSONP Support
var url = "http://192.168.0.103:12075/api/test.aspx?id=1&callback=?";
Jquery.getjson (URL, function (data) {
//process returned JSON data
alert ("Name:" + data.name+ ", Price:" + data.price);
The call webservices is used as follows:
$.getjson (
"http://192.168.0.103:12050/services/WebService1.asmx/ws/test?callback=?",
{name: "Jim", Time : "2pm"},
function (data) {
//processing returned JSON data
alert (decodeURI (data.msg));
});
Server side (webservices background code):
[WebMethod]
public void ws (String name,string time) {
HttpRequest Request = HttpContext.Current.Request;
String callback = request["callback"];
HttpResponse Response = HttpContext.Current.Response;
Response.Write (Callback + "({msg: ' Return Data '}");
Response.End ();
}
Summary JSONP:
Two points should be noted:
1. Does not provide the error handling mechanism, has the data returns, does not display the content, obtains a 404 error
2. The use of untrusted services can pose a significant security risk.
Json
JSON is a lightweight data interchange format compared to XML. Easy for people to read and write. It is also easy to machine parse and generate, divided into two types: objects (using the following point syntax to get property values) and arrays (traversal array, index fetch).
1. Convert JSON text to JavaScript objects
Reads JSON data from a Web server, converts the JSON data to a JavaScript object, and then uses that data in a Web page.
The JavaScript function eval () can be used to convert JSON text to JavaScript objects. You must enclose the text in parentheses in order to avoid grammatical errors.
That is, the JSON string is converted to a JSON object through the Eval function, and can then be accessed in two ways:
Server side:
The user entity object, which is serialized in the background as an object for the foreground to use
string Strjson = Newtonsoft.Json.JsonConvert.SerializeObject (user);
Context. Response.Write (Strjson);
Front desk:
$.getjson (
"webdata/test.aspx",
function (data) {
//Get a value inside the object (username: User name)
$ ("#divmessage"). Text (data.username);
}
);
2. Array capture
Packjson = [
{"Name": "Nikita", "Password": "1111"},
{"name": "Tony", "Password": "2222"}
];
Looping through:
for (var i = 0; i < packjson.length i++) {
alert (packjson[i].name + "" + Packjson[i].password);
}
The above is a small set for you to introduce a simple summary of JSON and JSONP, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!