2π
β
The assignment of jsoninfo
is happening outside of the for-loop but the assignment of geoinfo
is happening inside of it. You need to aggregate all of the geoloc.attrib
values into a list and convert this to json at the end:
def xml_parser(request):
infos = []
for child in root.findall('GetAll'):
for geoloc in child.iter('loc'):
infos.append(geoloc.attrib)
jsoninfo = json.dumps(infos, ensure_ascii=False)
return HttpResponse(jsoninfo, content_type='application/json')
This assumes you actually meant to output a single JSON object as your response instead of two encodings of independent objects separated by a newline character.
π€David Sanders
0π
You are overwriting geoinfo
every time in your loop. You are not storing all the results. Consider creating an empty list, appending to it, and sending that back as a response
π€sedavidw
Source:stackexchange.com