what is the use of json in .net

有人可以解释什么是json方法,它在.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.

链接地址: http://www.djcxy.com/p/47832.html

上一篇: 将JavaScript字符串或数组转换为JSON对象

下一篇: .net中的json有什么用处