JSON - 从数组加入值

时间:2017-07-18 19:59:07

标签: python arrays json python-3.x

如何组合键text的两个值并在其间返回“\ n”键?我希望输出显示如下。

{ "text": "Hi ! Nice to meet you !\nMy name is Robot." }

这是JSON结构:

{
"output": {
  "log_messages": [],
  "text": [
    "Hi ! Nice to meet you !",
    "My name is Robot."
  ],
  "nodes_visited": [
    "Hello"
  ]
}

我想出了如何返回两个键,但不知道如何将它们作为一个字符串返回。谢谢!

1 个答案:

答案 0 :(得分:2)

with open(filename) as f:
    obj = json.load(f)

output = {'text': '\n'.join(obj['output']['text'])}
print output
#    {'text': 'Hi ! Nice to meet you !\nMy name is Robot.'}