Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python在嵌套json中创建对象?_Python_Json - Fatal编程技术网

如何使用python在嵌套json中创建对象?

如何使用python在嵌套json中创建对象?,python,json,Python,Json,如何使用python在嵌套json中创建对象 范例 { friends:{ john:{ height:182 } } } 例如,我将如何在john内部创建另一个名为weight的对象,并使用python将其设置为60kg(只是一个示例) data = {'friends':{'john':{'height':182}}} data['friends']['john']['weight'] = 60 输出: {'fri

如何使用python在嵌套json中创建对象

范例

{
    friends:{
        john:{
            height:182
        }
    }
}

例如,我将如何在john内部创建另一个名为weight的对象,并使用python将其设置为60kg(只是一个示例)

data = {'friends':{'john':{'height':182}}}
data['friends']['john']['weight'] = 60
输出:

{'friends': {'john': {'height': 182, 'weight': 60}}}

您可以执行以下操作:

In [1]: x = {
    "friends":{
        "john":{
            "height": 182
     }

In [2]: x["friends"]["john"]["weight"] = 60

In [3]: x
Out[3]: {'friends': {'john': {'height': 182, 'weight': 60}}}

这既不是可运行的Python代码示例,也不是有效的JSON语法。请出示一张真实的支票。您必须按照
数据['friends']['john']['weight']=60
使用一些代码,但这很难说。