import json
import random
import collections
filename = '在线请求0426.txt'
# 将数据加载到一个列表中
#首先得到所有的商家,把商家设置一个状态,存到一个map里
poiid_status_maps={}
poiid_shelf_maps= collections.defaultdict(list)
shelf_poiid_maps= collections.defaultdict(list)
with open(filename) as f:
origin_data1 = json.load(f)
reqItemList = origin_data1["reqItemList"]
for i in range(len(reqItemList)):
subItem_list = reqItemList[i]["subItem"] # 货架下的商家list
for j in range(len(subItem_list)):
poi_id = subItem_list[j]["itemId"]
if poi_id not in poiid_status_maps:
poi_status_index = random.randint(0,2)
poiid_status_maps[poi_id] = str(poi_status_index)
poiid_shelf_maps[poi_id].append(reqItemList[i]["itemId"])
shelf_poiid_maps[reqItemList[i]["itemId"]].append(poi_id)
print(poiid_shelf_maps)
print(shelf_poiid_maps)
with open(filename) as f:
origin_data = json.load(f)
#在请求中加入商家状态,reqItemList->subItem->attribute
reqItemList = origin_data["reqItemList"]
print(reqItemList)
for i in range(len(reqItemList)):
subItem_list = reqItemList[i]["subItem"] #货架下的商家list
for j in range(len(subItem_list)):
cur_poi_id = subItem_list[j]["itemId"]
cur_poi_id_status = poiid_status_maps[cur_poi_id]
maps1={}
maps1["poiStatus"] = cur_poi_id_status
subItem_list[j]["attribute"] =maps1
print(origin_data)
# res_path = "在线请求0426_test1.txt"
res_path = open('在线请求0426_test1.json','w',encoding='utf-8')
json.dump(origin_data, res_path)
|