正如我和其他答案所提到的,json 是处理非结构化数据的起点。
从解析你的字符串开始...
import json
json_str = """{
"idannonce" : "121130815",
"idagence" : "113840",
"idtiers" : "169816",
"typedebien" : "Appartement",
"typedetransaction" : ["vente"],
"idtypepublicationsourcecouplage" : "SL",
...
"si_sdEau" : "0",
"nb_photos" : "6",
"prix" : "745000",
"surface" : "76"
}"""
json_data = json.loads(json_str)
print(json_data)
重要的是 json.loads 函数,它完成所有繁重的工作,将您的 json 字符串解码为实际的 python 对象。
由此,我们得到一个如下所示的dict 对象:
{'si_balcon': '1', 'affichagetype': [{'name': 'list', 'value': True}], 'codepostal': '75016', 'typedetransaction': ['vente'], 'naturebien': '1', 'etage': '1', 'position': '0', 'idtypechauffage': 'central', 'idtypecuisine': 'séparée', 'nb_photos': '6', 'prix': '745000', 'nb_pieces': '3', 'idtypecommerce': '0', 'idtypepublicationsourcecouplage': 'SL', 'si_sdEau': '0', 'codeinsee': '750116', 'cp': '75016', 'nb_chambres': '2', 'idagence': '113840', 'si_sdbain': '1', 'typedebien': 'Appartement', 'idannonce': '121130815', 'produitsvisibilite': 'AD:AC:AG:BB:AW', 'surface': '76', 'idtiers': '169816'}
现在,您可以通过循环访问所有数据,如下所示:
for key in json_data:
print(key, ':', json_data[key])
打印出来:
si_balcon : 1
affichagetype : [{'name': 'list', 'value': True}]
codepostal : 75016
typedetransaction : ['vente']
naturebien : 1
...
produitsvisibilite : AD:AC:AG:BB:AW
surface : 76
idtiers : 169816
等等。您只需执行json_data[someKey] 即可访问您想要的任何元素。