What is JSON and why would I use it?

前端 未结 17 1853
说谎
说谎 2020-11-21 15:54

I\'ve looked on wikipedia and Googled it and read the official documentation, but I still haven\'t got to the point where I really understand what JSON is, and why I\'d use

相关标签:
17条回答
  • 2020-11-21 16:28

    The Concept Explained - No Code or Technical Jargon

    What is JSON? – How I explained it to my wifeTM

    Me: “It’s basically a way of communicating with someone in writing....but with very specific rules.

    Wife: yeah....?

    Me: In prosaic English, the rules are pretty loose: just like with cage fighting. Not so with JSON. There are many ways of describing something:

    • Example 1: Our family has 4 people: You, me and 2 kids.

    • Example 2: Our family: you, me, kid1 and kid2.

    • Example 3: Family: [ you, me, kid1, kid2]

    • Example 4: we got 4 people in our family: mum, dad, kid1 and kid2.

    Wife: Why don’t they just use plain English instead?

    Me: They would, but remember we’re dealing with computers. A computer is stupid and is not going to be able to understand sentences. So we gotta be really specific when computers are involved otherwise they get confused. Furthermore, JSON is a fairly efficient way of communicating, so most of the irrelevant stuff is cut out, which is pretty hand. If you wanted to communicate our family, to a computer, one way you could do so is like this:

    {
        "Family": ["Me", "Wife", "Kid1", "Kid2"] 
    }
    

    ……and that is basically JSON. But remember, you MUST obey the JSON grammar rules. If you break those rules, then a computer simply will not understand (i.e. parse) what you are writing.

    Wife: So how do I write in Json?

    A good way would be to use a json serialiser - which is a library which does the heavy lifting for you.

    Summary

    JSON is basically a way of communicating data to someone, with very, very specific rules. Using Key Value Pairs and Arrays. This is the concept explained, at this point it is worth reading the specific rules above.

    0 讨论(0)
  • 2020-11-21 16:28

    I like JSON mainly because it's so terse. For web content that can be gzipped, this isn't necessarily a big deal (hence why xhtml is so popular). But there are occasions where this can be beneficial.

    For example, for one project I was transmitting information that needed to be serialized and transmitted via XMPP. Since most servers will limit the amount of data you can transmit in a single message, I found it helpful to use JSON over the obvious alternative, XML.

    As an added bonus, if you're familiar with Python or Javascript, you already pretty much know JSON and can interpret it without much training at all.

    0 讨论(0)
  • 2020-11-21 16:29

    The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML.

    0 讨论(0)
  • 2020-11-21 16:31

    Sometimes technicality is given where none is required, and while many of the top voted answers are accurately technical and specific, I personally don't think they are any more easy to understand, or succinct, as what can be found on Wikipedia, or in official documentation.

    The way I like to think of JSON is exactly what it is - a language within a world of different languages. However, the difference between JSON and other languages is that "everyone" "speaks" JSON, along with their "native language."

    Using a real world example, let's pretend we have three people. One person speaks Igbo as their native tongue. The second person would like to interact with the first person, however, the first person speaks Yoruba as their first language.

    What can we do?

    Thankfully, the third person in our example grew up speaking English, but also happens to speak both Igbo and Yoruba as second languages, and so can act as an intermediary between the first two individuals.

    In the programming world, the first "person" is Python, the second "person" is Ruby, and the third "person" is JSON, who just so happens to be able to "translate" Ruby into Python and vice versa! Now obviously this analogy isn't a perfect one, but, as someone who is bilingual, I believe it's an easy way to look at how JSON interacts with other programming languages.

    0 讨论(0)
  • 2020-11-21 16:32

    JSON(Javascript object notation) is a light weight data format for data exchange/transfer. Its in key value pair as the JavaScript is. For REST API its widely used for data transfer from server to client. Nowadays many of the social media sites are using this. Although I don't see this as robust as XML with respect of data types. XML has very rich datatypes and XSD. JSON is bit lacking in this.

    For same amount of string data JSON will be lighter compare to XML as XML has all that opening and closing tags, etc...

    0 讨论(0)
提交回复
热议问题