Python-将字符串转换为json并组合

发布于 2024-10-20 22:57:09 字数 721 浏览 4 评论 0原文

我从 api url data1 和 data2 获取了两个字符串,

pagehandle = urllib2.urlopen(ariel_url+"?%s"  % params1)
data1 = pagehandle.read(); 
pagehandle = urllib2.urlopen(ariel_url+"?%s"  % params2)
data2 = pagehandle.read(); 

其中包含以下 string

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180]]}

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["iphone", 49],["ipod", 590]]}

我想将这两个字符串转换为 json 并以输出的方式组合(或组合字符串然后转换) json 可能喜欢

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180],["iphone", 49],["ipod", 590]]]}

我该怎么做?我更喜欢使用 simplejson 库

I h've two strings got from an api url

pagehandle = urllib2.urlopen(ariel_url+"?%s"  % params1)
data1 = pagehandle.read(); 
pagehandle = urllib2.urlopen(ariel_url+"?%s"  % params2)
data2 = pagehandle.read(); 

data1 and data2 contain the following string

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180]]}

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["iphone", 49],["ipod", 590]]}

I want to convert these two strings into json and combine (or combine strings and then convert) in such a way that output json may like

{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180],["iphone", 49],["ipod", 590]]]}

How can i do this? i prefer to use simplejson library

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-10-27 22:57:09

这些不是字符串,而是字典。你可以像这样组合这些字典:

def combine(dict1, dict2):
    if dict1['priority'] == dict2['priority'] and dict1['titles'] == dict2['titles']:
        return {
            'priority': dict1['priority'],
            'titles': dict1['titles'],
            'values': dict1['values'] + dict2['values']
        }

之后你只需运行:

import json
json.dumps(combine(dict1, dict2))

你就会得到这两个组合字典的 json。

编辑

所以我明白你真正得到的是:

s1 = '{"priority":"HIGH", "titles":["Brand", "Likes"], values:[["addidas", 130],["nike", 180]]}'

s2 = '{"priority":"HIGH", "titles":["Brand", "Likes"], values:[["iphone", 49],["ipod", 590]]}'

在这种情况下你可以运行:

dict1 = json.loads(s1)
dict2 = json.loads(s2)
result = combine(dict1, dict2)

These are not strings, but dictionaries. You can combine those dictionary like this:

def combine(dict1, dict2):
    if dict1['priority'] == dict2['priority'] and dict1['titles'] == dict2['titles']:
        return {
            'priority': dict1['priority'],
            'titles': dict1['titles'],
            'values': dict1['values'] + dict2['values']
        }

after that you simply run:

import json
json.dumps(combine(dict1, dict2))

and you'll get a json of those two combined dictionaries.

EDIT

So I understand is what you really got is:

s1 = '{"priority":"HIGH", "titles":["Brand", "Likes"], values:[["addidas", 130],["nike", 180]]}'

s2 = '{"priority":"HIGH", "titles":["Brand", "Likes"], values:[["iphone", 49],["ipod", 590]]}'

In this case you can run:

dict1 = json.loads(s1)
dict2 = json.loads(s2)
result = combine(dict1, dict2)
关于从前 2024-10-27 22:57:09

干得好:

import json
a = json.loads('{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180]]}')
b = json.loads('{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["iphone", 49],["ipod", 590]]}')

for value in a['values']:
   b['values'].append(value)

# edited
json.dumps(b)

Here you go:

import json
a = json.loads('{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["addidas", 130],["nike", 180]]}')
b = json.loads('{"priority":"HIGH", "titles":["Brand", "Likes"], "values":[["iphone", 49],["ipod", 590]]}')

for value in a['values']:
   b['values'].append(value)

# edited
json.dumps(b)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文