我的JSON数据是这种格式。
[
{
"id": "532befe4ee434047ff968a6e",
"company": "528458c4bbe7823947b6d2a3",
"values" : [
{
"Value":"11",
"uniqueId":true
},
{
"Value":"14",
"uniqueId":true
},
]
},
{
"id": "532befe4ee434047ff968a",
"company": "528458c4bbe7823947b6d",
"values" : [
{
"Value":"1111",
"uniqueId":true
},
{
"Value":"10",
"uniqueId":true
},
]
}
]
如果我想根据公司字段进行过滤,则可以通过这种方式进行。
qaresults = QAResult.objects.filter(company= comapnyId)
它给了我第一本清单字典
但是,如果我想根据第一个字典的Value Key的值列表的“值”对此进行过滤,该怎么办?
我不是100%确定您想要什么,但是据我了解您的问题,请尝试以下解决方案:
import json
json_dict = json.loads('[{"id": "532befe4ee434047ff968a6e","company": "528458c4bbe7823947b6d2a3","values": [{"Value": "11","uniqueId": true},{"Value": "14","uniqueId": true}]},{"id": "532befe4ee434047ff968a","company": "528458c4bbe7823947b6d","values": [{"Value": "1111","uniqueId": true},{"Value": "10","uniqueId": true}]}]')
expected_values = []
js = json_dict[0]
for key,value in js.items():
if key == 'values':
expected_values.append(value[0]['Value'])
进而
qaresults = QAResult.objects.filter(company__id__in = expected_values)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句