简体   繁体   English

如何在 Python 中返回 JSON “真”

[英]How to return JSON “true” in Python

I have a simple python code that returns a JSON object with one boolean fields and two others.我有一个简单的 python 代码,它返回一个 JSON object 和一个 Z84E2C64F38F78BA3EAZ775C905AB5A2DA2 字段和其他两个字段。 The goal is to get the following response目标是得到以下响应

{
    "prop": "foo",
    "name": "bar",
    "isSomething": true
}

I need a valid JSON true , but the python response gives me a JSON string "true" .我需要一个有效的 JSON true ,但是 python 响应给了我一个 JSON 字符串"true"

{
    "prop": "foo",
    "name": "bar",
    "isSomething": "true"
}

My Python code is:我的 Python 代码是:

import json
x = { 
        'prop' : 'foo',
        'name': 'bar',
        'isSomething': json.dumps(True)
    }  
return x

I am executing this code using Python 3.7.我正在使用 Python 3.7 执行此代码。

You should json.dumps the entire x object:你应该json.dumps整个x object:

import json
x = { 
        'prop' : 'foo',
        'name': 'bar',
        'isSomething': True
    }  
return json.dumps(x)

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

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