2017-04-03 115 views
-2

其实我从一个python对象开始构建Json对象。Python构建了混合类型的JSON

我的出发JSON是:

responseMsgObject = {'Version': 1, 
         'Id': 'xc23', 
         'Local': "US" 
         'Type': "Test", 
         'Message' : "Message body" } 

    responseMsgJson = json.dumps(responseMsgObject, sort_keys=False) 

每个东西的作品,但现在我需要把JSON以下,进入“Message”字段。

{ 
    "DepID": "001", 
    "Assets": [ 
    { 
     "Type": "xyz", 
     "Text": [ 
     "abc", 
     "def" 
     ], 
     "Metadata": { 
     "V": "1", 
     "Req": true, 
     "Other": "othervalue" 
     }, 
     "Check": "refdw321" 
    }, 
    { 
     "Type": "jkl", 
     "Text": [ 
     "ghi" 
     ], 
     "Metadata": { 
     "V": "6" 
     }, 
     "Check": "345ghsdan" 
    } 
    ] 
} 

我建立了许多其他json(但更简单),但我在这个json的麻烦。

感谢您的帮助。

+1

所以正常工作,有什么问题呢? –

+0

请编辑该问题以提供[最小,完整和可验证的示例](https://stackoverflow.com/help/mcve),具体包括您正在努力工作的部分以及相关的堆栈跟踪(如果有)。 – roganjosh

+0

我更新我的问题 – Federico

回答

1

尝试用真实来代替真正的我

import json 
responseMsgObject = { 
    'Version': 1, 
    'Id': 'xc23', 
    'Local': "US", 
    'Type': "Test", 
    'Message': { 
     "DepID": "001", 
     "Assets": [{ 
      "Type": "xyz", 
      "Text": [ 
       "abc", 
       "def" 
      ], 
      "Metadata": { 
       "V": "1", 
       "Req": True, 
       "Other": "othervalue" 
      }, 
      "Check": "refdw321" 
     }, { 
      "Type": "jkl", 
      "Text": [ 
       "ghi" 
      ], 
      "Metadata": { 
       "V": "6" 
      }, 
      "Check": "345ghsdan4" 
     }] 
    } 
} 

responseMsgJson = json.dumps(responseMsgObject, sort_keys=False) 
print("responseMsgJson", responseMsgJson) 

DEMO

+0

我需要把这个JSON放入另一个JSON中。具体到“消息”字段 – Federico

+0

我用第二个json替换第一个json中的Message body是否正确? – David

+0

{'Version':1,'Id':'xc23','Local':'US','Type':'Test','Message':这里是第二个JSON} – Federico