- #1038
- 01 Jan 2019
What is JSON?
JSON (JavaScript Object Notation) is a format of how data is structured. It's easy to read and write by the programming languages and it's also verry understandable for humans. In short, the format is a collection of key-value pairs which are separated by colons and commas. Let's look at the example:
{
"firstName": "John",
"lastName": "Smith",
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
}
}
The "key-value" pairs are separated by colons and new elements are separated by commas. Every "value" can contain many "key-value" pairs so the elements can be nested in each other.
How to wrtie in JSON ?
We write our programs in Python so we'll look at that how json is generated in Python. First of all we need to import the json module.
import json
Then we create an object that will contais our JSON
data = {}
After that we create our first key
data['firstname'] = []
Then the value for this key
data['firstname'] = "John"
In the same name we add the last name.
data['lastname'] = [] data['lastname'] = "Smith"
And here comes the "Address" which is a little bit more complicated but not enough to make the things hard. We can do it in two ways. Let's look at them.
First way: Create new key called "address" and add key-value pairs to his value. Sounds complicated but it's not.
data['address'] = [] data['address'] = {"streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": 10021}
That was easy right ? Second way: create another object for your address and add it to the main.
data['address'] = [] myAddress= {} myAddress["streetAddress"] = [] myAddress["streetAddress"] = "21 2nd Street" ... (here we repeat the last line for the other key-values) ... data['address'] = myAddress
And the last thing we had to do is to convert the JSON to string. Why ? Because the string is more easier way to transfer some information. And because it's easier to send it over internet than the sending of object. At the end JSON is only format not a type of object.
json.dumps(data)
How to read JSON ?
Reading is more easier even than the writing.
newData = json.loads(data)
That's it! Now, if you want to print some of the elements you just have to write
print(newData['firstName'])
And that will show you
"John"
If you want to print all the iformation you can do it like that:
print(newData)
Usually the JSON strings are big, so you don't want to print them all, only some of their elements.
Курсове и занятия включващи този Урок
Този Урок е използван в следните курсове и занятия.
Perfect STEM course. Module 2 - 1D Games with Led strip.
Get a shiny RGB LED-strip. Connect it to a power supply and smart computer like Raspberry Pi, and you'll have your own playground. Create your own games with your unique rules in only one dimesnion. Play.
That's the second part of our perfect course. You move through the content, we check it and return feedback to you.
In the end, you should be able to program and design 1D games that make your games more interesting than ever. For everybody age 12+, 16+, 21+, 35+, etc
- 19
- 0:00
- 37
Communication and Format of messages
There are some problems which apears when you want to parse messages from the server. Some of them are easy to solve, others need a little bit more attention. In practice people use format called JSON.
- 2
- 0
- 2
- 3d_rotation 0