将对象数组连接到 Python 中的字符串值
Join array of objects to string value in Python
我想将对象数组连接到 python 中的字符串。我有什么办法可以做到吗?
url = "https://google.com",
search = "thai food",
results = [
{
"restaurant": "Siam Palace",
"rating": "4.5"
},
{
"restaurant": "Bangkok Palace",
"rating": "3.5"
}
]
我希望能够将这些全部连接起来形成一个值。
如果我能让它看起来像:
data = { url = "https://google.com",
{
search = "thai food",
results = [
{
"restaurant": "Siam Palace",
"rating": "4.5"
},
{
"restaurant": "Bangkok Palace",
"rating": "3.5"
}
]
}
}
我从 mongodb 收到这些结果,想将这 3 个结果合并在一起。
使用JSON模块
data = {} # create empty dict
# set the fields
data['url'] = 'https://google.com'
data['search'] = 'thai food'
# set the results
data['results'] = results
# export as string
import json
print(json.dumps(data, indent=4)
我想将对象数组连接到 python 中的字符串。我有什么办法可以做到吗?
url = "https://google.com",
search = "thai food",
results = [
{
"restaurant": "Siam Palace",
"rating": "4.5"
},
{
"restaurant": "Bangkok Palace",
"rating": "3.5"
}
]
我希望能够将这些全部连接起来形成一个值。
如果我能让它看起来像:
data = { url = "https://google.com",
{
search = "thai food",
results = [
{
"restaurant": "Siam Palace",
"rating": "4.5"
},
{
"restaurant": "Bangkok Palace",
"rating": "3.5"
}
]
}
}
我从 mongodb 收到这些结果,想将这 3 个结果合并在一起。
使用JSON模块
data = {} # create empty dict
# set the fields
data['url'] = 'https://google.com'
data['search'] = 'thai food'
# set the results
data['results'] = results
# export as string
import json
print(json.dumps(data, indent=4)