json.dumps e json.loads com strings + typing.TypedDict
This commit is contained in:
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@@ -41,6 +41,6 @@
|
|||||||
"python.testing.pytestEnabled": true,
|
"python.testing.pytestEnabled": true,
|
||||||
"python.analysis.diagnosticSeverityOverrides": {},
|
"python.analysis.diagnosticSeverityOverrides": {},
|
||||||
// "python.defaultInterpreterPath": "./venv/bin/python",
|
// "python.defaultInterpreterPath": "./venv/bin/python",
|
||||||
"python.analysis.typeCheckingMode": "basic",
|
"python.analysis.typeCheckingMode": "strict",
|
||||||
"cSpell.enabled": true
|
"cSpell.enabled": true
|
||||||
}
|
}
|
||||||
|
|||||||
44
aula176.py
Normal file
44
aula176.py
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
# json.dumps e json.loads com strings + typing.TypedDict
|
||||||
|
# Ao converter de Python para JSON:
|
||||||
|
# Python JSON
|
||||||
|
# dict object
|
||||||
|
# list, tuple array
|
||||||
|
# str string
|
||||||
|
# int, float number
|
||||||
|
# True true
|
||||||
|
# False false
|
||||||
|
# None null
|
||||||
|
import json
|
||||||
|
# from pprint import pprint
|
||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
|
|
||||||
|
class Movie(TypedDict):
|
||||||
|
title: str
|
||||||
|
original_title: str
|
||||||
|
is_movie: bool
|
||||||
|
imdb_rating: float
|
||||||
|
year: int
|
||||||
|
characters: list[str]
|
||||||
|
budget: None | float
|
||||||
|
|
||||||
|
|
||||||
|
string_json = '''
|
||||||
|
{
|
||||||
|
"title": "O Senhor dos Anéis: A Sociedade do Anel",
|
||||||
|
"original_title": "The Lord of the Rings: The Fellowship of the Ring",
|
||||||
|
"is_movie": true,
|
||||||
|
"imdb_rating": 8.8,
|
||||||
|
"year": 2001,
|
||||||
|
"characters": ["Frodo", "Sam", "Gandalf", "Legolas", "Boromir"],
|
||||||
|
"budget": null
|
||||||
|
}
|
||||||
|
'''
|
||||||
|
filme: Movie = json.loads(string_json)
|
||||||
|
# pprint(filme, width=40)
|
||||||
|
# print(filme['title'])
|
||||||
|
# print(filme['characters'][0])
|
||||||
|
# print(filme['year'] + 10)
|
||||||
|
|
||||||
|
json_string = json.dumps(filme, ensure_ascii=False, indent=2)
|
||||||
|
print(json_string)
|
||||||
Reference in New Issue
Block a user