python json
import json
# some JSON:
x = '{ "name":"John", "age":30, "city":"New York"}'
# parse x:
y = json.loads(x)
# the result is a Python dictionary:
print(y["age"])
4
10
#load and print elements of a json file
import json
file = "my_json_file.json"
Json = json.load(open(file)) #Json is a dictionary
print(Json)
#OUTPUT: '{ "name":"John", "age":30, "city":"New York"}'
print("Hello",Json["name"])
#OUTPUT: Hello John
Thank you!
10
0
4
2
>>> import json
>>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]')
['foo', {'bar': ['baz', None, 1.0, 2]}]
>>> json.loads('"\\"foo\\bar"')
'"foo\x08ar'
>>> from io import StringIO
>>> io = StringIO('["streaming API"]')
>>> json.load(io)
['streaming API']
Thank you!
2
0
3
1
import json
json_file = json.load(open("your file.json", "r", encoding="utf-8"))
# For see if you don't have error:
print(json_file)
Thank you!
1
0
4.17
6
import json
# The json itself.
x = {
"name": "Jeff",
"age": "22",
"cars": [
{"model": "BMW 230", "mpg": 27.5},
{"model": "Ford Egde", "mpg": 27.5}
]
}
if x["cars"] != None: # Checking if cars is not None
car = x["name"]+ " has " + str(len(x["cars"])) + " cars: " # May be changed
for i in range(0, len(x["cars"])):
car += "Car "+ str(i+1) + ": " + x["cars"][i]["model"] + " that has a MPG of " + str(x["cars"][i]["mpg"]) + ", "
car = car[0:-2] + "."
print(car)
else:
print(x["name"] + " does not have any cars!")
Thank you!
6
0
4.3
10
{
"Icons":{
"app icon": "your icon.ico",
"eg icon": "eg.ico"
}
}
Thank you!
10
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types