What is JSON and why would I use it?

前端 未结 17 2216
说谎
说谎 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.

提交回复
热议问题