import json
json_value_tmp= json.loads(json_data) # api return value
for data in json_value_tmp:
a = data['name']
#json getdata from json file
import os
import json
# import pymysql
with open('haikou.json', encoding='utf-8') as a:
# 读取文件
result = json.load(a)
# 定义一个空数组
new_list = []
new_list_data = ''
# print(result)
# # 循环遍历
features = result['features']
j=0
for i in features: # i是个字典
# (i.get('name'), i.get('like'), i.get('address'))
# 熊猫 听歌 上海
# 老虎 运动 北京
j = j+1
print(i)
print(type(i))
print(i.get('properties').get('name'))
print(j)
new_list_name = str(i.get('properties').get('name'))
new_list_code = str(i.get('properties').get('code'))
newLine = new_list_name + '--' + new_list_code
new_list.append(newLine)
print(new_list) # [('熊猫', '听歌', '上海'), ('老虎', '运动', '北京')]
with open('your_file.txt', 'w',encoding='utf8') as f:
for item in new_list:
f.writelines(item)
f.write('\n')
# w 以写方式打开,
# a 以追加模式打开
# r+ 以读写模式打开
# w+ 以读写模式打开
# a+ 以读写模式打开