forked from lucianoAGit/Scripts-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanipulando-json.py
More file actions
35 lines (27 loc) · 858 Bytes
/
manipulando-json.py
File metadata and controls
35 lines (27 loc) · 858 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import json
x = {
"name": "John",
"age": 30,
"married": True,
"divorced": False,
"children": ("Ann","Billy"),
"pets": None,
"cars": [
{"model": "BMW 230", "mpg": 27.5},
{"model": "Ford Edge", "mpg": 24.1}
],
"itens": [
{"nome": "item 1", "preco": "12.30"},
{"nome": "item 2", "preco": "3.30"}
]
}
print(json.dumps(x) + "\n") # transforma o objeto em string corrida
print("Mostra um campo especifico: " + x['name'])
print("Mostra a lista de carros: " + str(x['cars']))
# Seleciona o 1 carro (linha zero) e a coluna model
print("Mostra o acesso a lista: " + str(x['cars'][0]['model']))
# Seleciona o 1 item (linha zero) e a coluna nome
print("Mostra o acesso a lista: " + str(x['itens'][0]['nome']))
# Mostra todos os nomes e precos da lista de itens
for item in x['itens']:
print(item['nome'] + " " + item['preco'])