您好,我正在尝试解析从 Android 发送的 Django 中的 JSON 数组 从 android 发送的 json 响应看起来像
[{"record":[{"intensity":"Low","body_subpart":"Scalp","symptom":"Agitation"}]}]
现在我在 django 中的功能如下:
record = simplejson.loads(request.POST['record'])
for o in record:
new_symptoms=UserSymptoms(health_record=new_healthrecord,body_subpart=o.body_subpart,symptoms=o.symptom,intensity=o.intensity)
new_symptoms.save()
但它不起作用 给我错误 为此,我还尝试在 python shell 中执行以上行
>>>rec=json.loads('[{"intensity":"Low","body_subpart":"Scalp","symptom":"Agitation"},{"intensity":"High","body_subpart":"Scalp","symptom":"Bleeding"}]')
>>> for o in rec:
... print rec.body_subpart
...
Traceback (most recent call last):
File "<console>", line 2, in <module>
AttributeError: 'list' object has no attribute 'body_subpart'
最佳答案
您必须使用 o['body_subpart']
而不是 o.body_subpart
。虽然这在 Javascript 中是相同的,但在 Python 中却有所不同。
关于android - 在django中解析json数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10061408/