What is JSON?
JSON (JavaScript Object notation), a common form of data representation in network communications, helps us present and exchange data in a self-describing, independent, and light way. This data can be easily and converted to JavaScript objects.
The biggest advantage of JSON format: it can be easily converted to a JavaScript object. For example, as seen in the code snippet below, we have a JSON-formatted data with "name", "Street", "Age" and "phone".
var jsonobject= {"name": "John Johnson","street": "Oslo West 555""age":,"Phone": " 555 1234567 "</script>
Now, this data can be used directly, such as we can get Jsonobject object, and it is convenient to call its "name" property.
Is it possible to do json the same thing with soap?
There are 2 different points:
First, soap is expressed as an XML tag, which is more heavy. JSON is relatively light, pure data.
Second, most importantly, JSON can be converted directly to JavaScript objects. The same is the tedious task of converting SOAP XML into JavaScript objects.
Does all the technology support JSON?
Yes, JSON is supported for almost all data exchanges.
As shown below:
WCF Service : If you want your WCF service to send a Json message instead of soap, you can set "Responseformat" as your operation contract "Webmessageformat.json".
[Operationcontract][webinvoke (Method="GET", uritemplate="/ GetData", requestformat=Webmessageformat.json, responseformat= Webmessageformat.json)]string GetData ();
MVC: If you want MVC to return JSON data, you can return a JSON-formatted customer object by calling JSON.
Public Jsonresult Customerjson () { Listnew list<customer>(); Thread.Sleep (the); New Customer (); " 1001 " ; Obj1. ADD (obj); return Json (obj1,jsonrequestbehavior.allowget);}
ASP: If you want to use ASP. NET to return JSON, we need to use the "DataContractJsonSerializer" class.
Newnew MemoryStream (); serializer. WriteObject (MS, MyPerson); string json ="application/json;charset=utf-8"; Response.Write (JSON); Response.End ();
How do I make a JSON call using jquery?
As in an MVC, the JSON object of employ can be returned through the "GetEmployee" function.
Public Jsonresult New "1001"; return Json (obj,jsonrequestbehavior.allowget);}
We use the Getjson function of jquery to invoke the JSON object returned by the above MVC function.
NULL , getData); function GetData (data) {alert (data.empcode);}
The Getjson function of jquery has three parameters:
- The first parameter, which gets the URL address of the JSON data.
- The second parameter is the MVC operation that can pass data to the URL. For now, we're just doing a get operation, so the second parameter is null.
- The third parameter, in JavaScript, uses a callback function to process the returned employee object.
Because the output is JSON, it automatically converts the JSON data into JavaScript objects. You can use the "getData" function directly to display the "Data.empcode" attribute, which is very convenient.
Can I post a JSON object to the server?
OK.
You can use jquery's "post" method to send data to the server.
The following is an example of how the post method is called, similarly, the post has 3 parameters:
- The first parameter, the URL address that accepts the JSON data
- The second parameter is the JSON object we want to send
- The third parameter, which is the callback function, we receive the response data for processing.
var mydata ={name: "Shiv", City: ' Mumbai '};$.post (/ / URL// Data to be Sentfunction// Call back function
How do I post a full HTML form to the server in JSON format?
First, we need to call jquery's "serialize" to serialize the form that needs to be post to a JSON object.
Then, use the similar method above to post.
The complete code is as follows:
var Mydata = $ ("#form1"). Serialize (); $.post ("/customer/getcustomer", JSON. Stringify (Mydata), displaydata);
In the background (server), JSON data streams can be captured through InputStream.
System.IO.Stream BODY =new System.IO.StreamReader (body); string s = reader. ReadToEnd ();
How can we convert a JSON string to a C # object?
To convert a JSON string to a C # object, we need to use the "JavaScriptSerializer" class, and using "deserialization", we convert the string to a C # object.
var jsonser = new JavaScriptSerializer () var obj = jsonser. Deserialize<dynamic> foreach (var x in obj) {String strvalue = X[“value”];}