简体   繁体   English

检查 boolean 的值 json Python

[英]check the boolean value of json Python

Hello I am pretty new working with AWS and SF I am trying to send information and I need to check if the list of json I am checking the information.您好,我是 AWS 和 SF 的新手,我正在尝试发送信息,我需要检查 json 的列表,我正在检查信息。 I have the next.json list:我有 next.json 列表:

here...b'[{
   "id": "xxxx",
   "success": true,
   "errors": []
   },
   {
   "id": "yyyy",
   "success": true,
   "errors": []
   }
  ]'

and in my lambda I do the next check:在我的 lambda 中,我进行下一项检查:

response = requests.request("PATCH", url, headers=headers, data=body)
 print('here...'+str(response.content))
if response.status_code == 200:
    for iResult in response.content.b["success"]:
        if iResult["success"] == false:
            raise Exception('Error. Check SF size fields...')

I want to make sure that every 'success' in every json is equals to True.我想确保每个 json 中的每个“成功”都等于 True。 And if it is false to raise an Exception.如果引发异常是错误的。 So I made a loop to iterate over each json but the problem I have is that I do not know how to access the json correctly.所以我做了一个循环来遍历每个 json 但我遇到的问题是我不知道如何正确访问 json。 What is confusing me is the " b' " in the json I print.令我困惑的是我打印的 json 中的“b”。 Could anybody help me?有人能帮帮我吗? Thanks谢谢

The b in the beginning means you have bytes, instead of a string , meaning you first have to convert your response content into a dictionary (think of a python term for a json) so that you can access the data in your response by their keys.开头的b表示您有字节,而不是字符串,这意味着您首先必须将响应内容转换为字典(想想 json 的 python 术语),以便您可以通过它们的键访问响应中的数据. Luckily that's easy to from a requests response with幸运的是,这很容易从请求响应中获得

json_data = response.json()
for sub_dict in json_data:
    if sub_dict ["success"] is False:
        raise ValueError("Check SF size fields...")

More about requests.response: https://requests.readthedocs.io/en/latest/api/#requests.Response有关 requests.response 的更多信息: https://requests.readthedocs.io/en/latest/api/#requests.Response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM