Python-将字符串转换为json并组合
我从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些不是字符串,而是字典。你可以像这样组合这些字典:
之后你只需运行:
你就会得到这两个组合字典的 json。
编辑
所以我明白你真正得到的是:
在这种情况下你可以运行:
These are not strings, but dictionaries. You can combine those dictionary like this:
after that you simply run:
and you'll get a json of those two combined dictionaries.
EDIT
So I understand is what you really got is:
In this case you can run:
干得好:
Here you go: