在Python中组合两个json字符串而不丢失任何数据
我试图将两个json字符串连接在一起,并将共享密钥列表合并在一起 简单地组合JSON字符串是不起作用的,因为您有两个有效的JSON字符串,它们构成无效的JSON字符串。从网上搜索看来,我必须把它们变成字典,并从那里对它们进行操作 我尝试了以下几点:在Python中组合两个json字符串而不丢失任何数据,python,json,Python,Json,我试图将两个json字符串连接在一起,并将共享密钥列表合并在一起 简单地组合JSON字符串是不起作用的,因为您有两个有效的JSON字符串,它们构成无效的JSON字符串。从网上搜索看来,我必须把它们变成字典,并从那里对它们进行操作 我尝试了以下几点: request = getServiceData(service, start_index, max_results) // This returns data from a valid query results = request.e
request = getServiceData(service, start_index, max_results) // This returns data from a valid query
results = request.execute()
//One attempt
combined_results = results
if results.get('nextLink'):
start_index = str(int(start_index) + int(max_results))
request = getServiceData(service, start_index, max_results)
results = request.execute()
new_combined_results = {}
for k,v in results.items():
pairs = zip(v, combined_results[k] + ['']) # add empty to avoid need for zip_longest()
flat = (item for sub in pairs for item in sub)
new_combined_results[k] = ''.join(flat)
combined_results = new_combined_results
print json.dumps(combined_results, indent=4)
上面的错误给出:TypeError:强制使用Unicode:需要字符串或缓冲区,找到列表
我试过这样的方法:
def merge(lsta, lstb):
for i in lstb:
for j in lsta:
if j['name'] == i['name']:
j.update(i)
break
else:
lsta.append(i)
for k,v in dictb.items():
merge(dicta.setdefault(k, []), v)
这也失败了,最后我尝试的另一种方法类似于:
z.copy(x)
z.update(y)
这也不起作用,我不相信合并本质上是两个dict或只是JSON(如果您使用JSON.dump)会如此困难
编辑:
根据要求,以下是一些使用start_索引值1和max_结果值1的示例数据:
第1页:
{
"kind": "analytics#gaData",
"rows": [
[
"A###",
"B###"
]
],
"containsSampledData": true,
"columnHeaders": [
{
"dataType": "STRING",
"columnType": "DIMENSION",
"name": "ga:browser"
},
{
"dataType": "STRING",
"columnType": "DIMENSION",
"name": "ga:operatingSystem"
}
],
"profileInfo": {
"webPropertyId": "###",
"internalWebPropertyId": "###",
"tableId": "ga:###",
"profileId": "###",
"profileName": "###",
"accountId": "###"
},
"itemsPerPage": 1,
"totalsForAllResults": {
"ga:pageviews": "###",
"ga:sessions": "###"
},
"nextLink": "https://www.googleapis.com/analytics/v3/data/ga?A###",
"sampleSize": "###",
"query": {
"max-results": 1,
"dimensions": "###",
"start-date": "###",
"start-index": 1,
"ids": "ga:###",
"metrics": [
"ga:sessions",
"ga:pageviews"
],
"end-date": "###"
},
"totalResults": ###,
"id": "https://www.googleapis.com/analytics/v3/data/gaA###",
"selfLink": "https://www.googleapis.com/analytics/v3/data/gaA###",
"sampleSpace": "###"
}
第2页:
{
"kind": "analytics#gaData",
"rows": [
[
"1###",
"2###"
]
],
"containsSampledData": true,
"columnHeaders": [
{
"dataType": "STRING",
"columnType": "DIMENSION",
"name": "ga:browser"
},
{
"dataType": "STRING",
"columnType": "DIMENSION",
"name": "ga:operatingSystem"
}
],
"profileInfo": {
"webPropertyId": "###",
"internalWebPropertyId": "###",
"tableId": "ga:###",
"profileId": "###",
"profileName": "###",
"accountId": "###"
},
"itemsPerPage": 1,
"totalsForAllResults": {
"ga:pageviews": "###",
"ga:sessions": "###"
},
"nextLink": "https://www.googleapis.com/analytics/v3/data/ga?1###",
"sampleSize": "###",
"query": {
"max-results": 1,
"dimensions": "###",
"start-date": "###",
"start-index": 1,
"ids": "ga:###",
"metrics": [
"ga:sessions",
"ga:pageviews"
],
"end-date": "###"
},
"totalResults": ###,
"id": "https://www.googleapis.com/analytics/v3/data/ga1###",
"selfLink": "https://www.googleapis.com/analytics/v3/data/ga1###",
"sampleSpace": "###"
}
合并:
{
"kind": "analytics#gaData",
"rows": [
[
"A###",
"B###"
],
[
"1###",
"2###"
]
],
"containsSampledData": true,
"columnHeaders": [
{
"dataType": "STRING",
"columnType": "DIMENSION",
"name": "ga:browser"
},
{
"dataType": "STRING",
"columnType": "DIMENSION",
"name": "ga:operatingSystem"
}
],
"profileInfo": {
"webPropertyId": "###",
"internalWebPropertyId": "###",
"tableId": "ga:###",
"profileId": "###",
"profileName": "###",
"accountId": "###"
},
"itemsPerPage": 1,
"totalsForAllResults": {
"ga:pageviews": "###",
"ga:sessions": "###"
},
"nextLink": {
"https://www.googleapis.com/analytics/v3/data/ga?A###",
"https://www.googleapis.com/analytics/v3/data/ga?1###"
},
"sampleSize": "###",
"query": {
"max-results": {
###,
###
}
"dimensions": "###",
"start-date": "###",
"start-index": {
###,
###
}
"ids": "ga:###",
"metrics": [
"ga:sessions",
"ga:pageviews"
],
"end-date": "###"
},
"totalResults": ###,
"id": {
"https://www.googleapis.com/analytics/v3/data/gaA###",
"https://www.googleapis.com/analytics/v3/data/ga1###",
}
"selfLink": {
"https://www.googleapis.com/analytics/v3/data/gaA###",
"https://www.googleapis.com/analytics/v3/data/ga1###",
}
"sampleSpace": "###"
}
或者类似的东西…
压缩包内的组合结果[k]+[''']
?您正在连接来自组合结果[k]
的值,并添加[''']
(列表)您是否假设具有单个响应(例如containsSampledData)的JSON属性始终等效/将被忽略/变为列表?事实上,看看这两个输入和输出,你根本不想把它们组合在一起——有些是一种工作方式,有些是另一种工作方式,比如nextLink,它神奇地变成了一个列表,而ColumnHeader却没有。您需要使用Json。加载此数据并以这种方式混合数据执行一些手动操作。没有现成的循环,即使列表理解也需要一个参数列表来执行动作X,其他的则需要动作Y。。。。