我想从API获取JSON响应,并使用所选字段给出响应。我已经获取了响应,但是无法使用新值创建响应。我对python世界和学习阶段非常陌生。
def search(request):
if request.method == 'POST':
searchQry = request.POST.get("searchQry","")
nodeIP = settings.NODEIP
params = {'search':searchQry}
apiResponse = requests.get(url = nodeIP, params = params)
data = apiResponse.json()
newArray = {}
nodeName = 'RPID'
if nodeName == 'RPID':
for x in data:
newArray['cphNumber'] = x["data"]["cphNumber"]
newArray['farmName'] = x['data']['farmName']
newArray['addressLine1'] = x['data']['addressLine1']
return HttpResponse(json.dumps(newArray))
else:
return HttpResponse('Unauthrozed Access')
我的响应数组如下所示:
[{“ data”:{“ cphNumber”:“ 321”,“ farmName”:“ 313”,“ addressLine1”:“ 13”,“ addressLine2”:“ 13”,“ region”:“ 13”, “ postalCode”:“ 13”},“ id”:“ 4c1b935664e6f684e89ee363f473ce3567599d4b9da0f5889565d5b6f0b84440”},{“ data”:{“ cphNumber”:“ 321”,“ farmName”:“ 313”,“ addressLine1”:“ 13”,“ addressLine2 “:” 13“,” region“:” 13“,” postalCode“:” 13“},” id“:” 7cbe7be9797896545410ed6c4dcc18064525037bc19fbe9272f9baabbb3216ec“},{” data“:{” cphNumber“:” 321“,” farmName“: “ 313”,“ addressLine1”:“ 13”,“ addressLine2”:“ 13”,“ region”:“ 13”,“ postalCode”:“ 13”},“ id”:“ 7df10c0b7b84434d5ace6811a1b2752a5e5bca13b691399ccac2a6ee79d17797”}]
作为回应,我只得到一个数组。我知道我必须做类似newArray[0]['cphNumber']
的事情,但是我遇到了错误。您能帮我解决这个问题吗?