The article directories

      • What is the JSON
      • A simple JSON object
      • A simple JSON array
      • A slightly more complex JSON object
      • A very complex JSON string

What is the JSON

Recently, I am making a desktop weather clock based on ESP8266 and STM32, which needs to use JSON parsing, so THE system will learn ABOUT JSON. In this article, we will first understand the basic format of JSON.

JSON: JavaScript Object Notation is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON uses a completely language-independent text format, but also uses conventions similar to the C language family (C, C++, C#, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy for humans to read and write, but also easy for machines to parse and generate (generally used to speed up network transmission). JSON parsers and JSON libraries support many different programming languages. The JSON text format is syntactically identical to the code that creates JavaScript objects. Because of this similarity, JavaScript programs can use the built-in eval() function to generate native JavaScript objects from JSON data without the need for a parser. JSON is a syntax for storing and exchanging text information. Similar to XML. JSON is smaller, faster, and easier to parse than XML. JSON is self-descriptive, concise, and easy to understand.

JSON is basically a string, but the elements are annotated with a specific notation, and many APIS return information in JSON format.

{} double brackets denote objects.

    • Inside the “” double quotes are attributes or values.
    • The colon indicates that the latter is the former value (this value can be a string, a number, or another JOSN array or JSON object)

[] Brackets represent arrays that can contain multiple JSON objects

So {” name “:” Michael “} can be interpreted as a JSON object that contains a single key/value pair, or an object that can contain multiple key/value pairs.

A simple JSON object

{
	"name": "Andy",  
	"age": 20     
}
Copy the code

This JSON object contains two key-value pairs.

A simple JSON array

[{
	"name": "Michael"
}, {
	"name": "Jerry"
}]
Copy the code

JSON array containing two JSON objects.

A slightly more complex JSON object

{ "success": "1", "result": { "timestamp": "1542456793", "datetime_1": "2018-11-17 20:13:13", "datetime_2": "On November 17, 2018, 20 13 points and 13 seconds,", "week_1" : "6", "week_2" : "Saturday", "week_3" : "Saturday", "week_4" : "Saturday"}}Copy the code

This is a free API used to get Beijing standard time address, return value is JSON string. The JSON object contains two key-value pairs, one key success, which corresponds to a string, and the other key result, which corresponds to a JSON object. So you can see that the value of the key can be a string, a number, another JSON object or a JSON array.

A very complex JSON string

{" results ": [{" location" : {" id ":" WS10730EM8EV ", "name" : "shenzhen", "country" : "CN", "path" : "shenzhen, shenzhen, guangdong, China", "timezone" : "Asia/Shanghai", "timezone_offset" : "+ 08:00"}, "now" : {" text ":" cloudy ", "code" : "4", "temperature" : "23" }, "last_update": "2018-11-17T20:05:00+08:00" }] }Copy the code

This is the JSON format of information returned from the 3-day weather forecast. It contains a key-value pair, the key is Results, and the value is an array.

The next article will show you how to use the cJSON library to parse JSON and get the values you want.

  • Online JSON format verification tool: BEJSON
  • Commonly used API data interface: K780 data
  • Free API: All kinds of unlimited free API interfaces

Selected historical articles:


Welcome to my personal blog

Or scan the code on wechat to follow my public number