python 读取json文件

import json

json_data = """{
   "favourite":{
      "bkmrk":{
         "id1490843709594066":{
            "guid":"904eff52277f403b89f6410fe2758646.11",
            "lcate":"1"
         },
         "id1490843712805183":{
            "guid":"58457f60eca64025bc43a978f9c98345.16",
            "lcate":"2"
         },
         "id149084371467327":{
            "guid":"a0f907f9dc8b40f689b083f3eba7228b.16",
            "lcate":"3"
         },
         "id1490843716295393":{
            "guid":"eb75d929455e468bb712e7bc2025d11a.16",
            "lcate":"4"
         }
      }
   }
}"""

data = json.loads(json_data)
for v in data['favourite']['bkmrk'].values():
    print("%s;%s" % (v['lcate'],  v['guid']))

输出

1;904eff52277f403b89f6410fe2758646.11
2;58457f60eca64025bc43a978f9c98345.16
3;a0f907f9dc8b40f689b083f3eba7228b.16
4;eb75d929455e468bb712e7bc2025d11a.16

取出特定数据

import json
with open("./1.json",'r',encoding='utf8') as fp:
    json_data = json.load(fp)
    print('这是文件中的json数据:',json_data)
    print('这是读取到文件数据的数据类型:', type(json_data))
    for v in json_data['tianqi'].values():
        print(v)
        # 取出特定数据
        print("%s,%s"%(v['filename'],v['variable']))

输出

{'filename': 'at0101.07422', 'variable': '气温', 'Highest_temp': '最高气温', 'Lowest_temp': '最低气温'}
at0101.07422,气温
{'filename': 'bp0101.07422', 'variable': '气压', 'Highest_pre': '最高气压', 'Lowest_pre': '最低气压'}
bp0101.07422,气压
{'filename': 'hu0101.07422', 'variable': '相对湿度', 'Highest_h': '最大相对湿度', 'Lowest_h': '最低相对湿度'}
hu0101.07422,相对湿度

你可能感兴趣的:(python)