I have a json format that looks like the following and I wish to deserialize it with python using import json
{
"FATHERFIELD 1": [
{
"childfield 1":[
],
"childfield 2": [
{
"a": 1,
"b": 2,
"c": {
"c_1": 3
}
}
]
}
],
"FATHERFIELD 2": [
]
}
In python, how would one use the json.loads() function to call the respective fields? Specifically, if I used
foo = json.loads(childfield2)
what kind of object would this be in python?
You have one JSON document ; you'd either read that document into a Python string (from a file or network connection) or open a file object for JSON data stored on disk, and decode it using the json
module . json.loads()
decodes JSON data from a string, json.load()
does so from a file object.
Once loaded, you have a Python object; a dictionary containing other objects, including more dictionaries and lists. You'd address these like any other Python object:
dict_from_json['FATHERFIELD 1'][0]['childfield 1']
addresses a key in the first dictionary in the list addressed by the 'FATHERFIELD 1'
key in the top-level dictionary.
import json
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004,"
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
print(json.myfamily)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.