【发布时间】:2020-06-05 03:43:39
【问题描述】:
我从 RESTful API 获得了经纬度。我需要从另一个 json 字段中创建新的 json 字段。
我的代码如下:
def collect_data():
data = requests.get(url = URL).json()
del data['positions'][1]
my_dict={}
my_dict["satlatitude"]= data["positions"][0]["satlatitude"]
my_dict["satlongitude"]= data["positions"][0]["satlongitude"]
new_data = json.dumps(my_dict)
new_data2 = {'geo' : {"lat": new_data["satlatitude"][0], "lon": new_data["satlatitude"][0]}}
es.index(index='satellitepositions', doc_type='satelitepos', body=new_data2)
schedule.every(10).seconds.do(collect_data)
while True:
schedule.run_pending()
time.sleep(1)
我没有得到我所期望的。我需要的是:
new_data2 = {'geo' : {"lat":satlatitude , "lon": satlongitude}}
这个satlatitude和satlongitude应该来自数据。我很困惑如何提取字段
来自 json 文档。
有人可以帮我解决这个问题吗?
RESTful 数据样本:
Json 文档: {'info': {'satname': 'SPACE STATION', 'satid': 25544,
'transactionscount': 0}, '仓位': [{'satlatitude': 28.89539607,
'satlongitude': 90.44547739, 'sataltitude': 420.36, '方位角': 12.46,
“海拔”:-52.81,“ra”:215.55022984,“dec”:-5.00234017,“时间戳”:
1591196844, '黯然失色': True}]}
new_data2 = {'geo' : {"lat":28.89539607 , "lon": 90.44547739,}}
但是这些值每次都应该改变。
【问题讨论】:
-
能否提供 requests 查询返回的数据示例?如果不了解您所操作的数据,就很难为您提供帮助。
-
是的,我会给