what is the use of json in .net
Can someone explain what开发者_StackOverflow is json method and it use in .net
JSON is not .NET specific, it is a form of data transmission. It can be likened to an array of strings. Its main use is to provide a means of returning data from a web service.
Data from a web service (prior to JSON) was predominantly done with XML. But XML is costly to serialise/deserialise because of the complex traversal of the document.
Because of the simple format of JSON, its much faster to serialise/deserialise, not to mention its a smaller piece of data which means its faster over the wire.
None of the above points have anything to do with .NET, they just pertain to JSON in the world of web services.
Now in relation to ASP.NET:
You most likely either have a "classic" web service (ASMX), or a WCF web service, and want to return data from it. Without any extra configuration, youre web service would return XML. But this can be changed to JSON with a few steps (google 'return json from .net web service). The most common use of this is invoking a webservice with AJAX (and/or jQuery) on an ASP.NET page, in which having your data returned as JSON as opposed to XML will benefit for the above reasons.
JSON is this:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
It's a relatively (to XML) lightweight method of transmitting data over the web (consuming a web service).
The XML representation of the same data is more protracted:
<Person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<age>25</age>
<address>
<streetAddress>21 2nd Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumber type="home">212 555-1234</phoneNumber>
<phoneNumber type="fax">646 555-4567</phoneNumber>
</Person>
There is good support for it in jQuery ($.getJSON() method) and in ASP.NET MVC (return a JSONResult from an action). That's why many .NET developers are led to the impression that it's a bespoke .NET technology; it's not, it's simply one that has been embraced by .NET.
As the name suggests, the technology JSON normally relies upon is Javascript (although it is language-independant, like XML). The server-side just depends on the web service returning JSON data as a result.
JSON could be used to serialize data into an interoperable format which is particularly well suited for consumption by browser. In .NET you could write a WCF service which exposes data using JSON. Another flavor of JSON is JSONP which allows cross domain AJAX calls.
JSON is a way to send the data to java script to use it. For instance, I send a list of objects from app side to java script side of the web application project, then I eval it and I get the same structure of the list of object which means you may reach all the properties of object as you do in application side using c#.
Few things to add...
For the XML you need an XML parser, when all major browser already have a JSON parser built in - so you don't have to worry about serialization/deserialization.
You also save on the traffic (bandwidth) and in my opinion it's easier to read.
精彩评论