解析json文件并将字符串添加到URL

时间:2013-07-16 13:43:35

标签: python

如何解析json输出只从数据中获取列表,然后将输出添加到say google.com/confidetial和列表中的其他字符串中。 所以我的json out put我将它命名为“text”

text = {"success":true,"code":200,"data":["Confidential","L1","Secret","Secret123","foobar","maret1","maret2","posted","rontest"],"errs":[],"debugs":[]}.

我要做的是仅在数据下获取列表。到目前为止,我得到的剧本给了我整个json out put。

json.loads(text)
print text 
output = urllib.urlopen("http://google.com" % text)
print output.geturl()
print output.read()

3 个答案:

答案 0 :(得分:3)

jsonobj = json.loads(text)
print jsonobj['data']

将在JSON的data部分打印列表。

如果您想在google.com之后打开每个链接,可以试试这个:

def processlinks(text):
    output = urllib.urlopen('http://google.com/' % text)
    print output.geturl()
    print output.read()

map(processlinks, jsonobj['data'])

答案 1 :(得分:0)

info = json.loads(text)
json_text = json.dumps(info["data"])

使用json.dumps将从json.loads获取的python数据结构转换回常规json文本。

因此,您可以在之前使用json_text的任何地方使用text,在您的情况下,它应该只有选定的密钥:"data"

答案 2 :(得分:0)

也许这样的地方result是你的JSON数据:

from itertools import product

base_domains = ['http://www.google.com', 'http://www.example.com']
result = {"success":True,"code":200,"data":["Confidential","L1","Secret","Secret123","foobar","maret1","maret2","posted","rontest"],"errs":[],"debugs":[]}
for path in product(base_domains, result['data']):
    print '/'.join(path) # do whatever

http://www.google.com/Confidential
http://www.google.com/L1
http://www.google.com/Secret
http://www.google.com/Secret123
http://www.google.com/foobar
http://www.google.com/maret1
http://www.google.com/maret2
http://www.google.com/posted
http://www.google.com/rontest
http://www.example.com/Confidential
http://www.example.com/L1
http://www.example.com/Secret
http://www.example.com/Secret123
http://www.example.com/foobar
http://www.example.com/maret1
http://www.example.com/maret2
http://www.example.com/posted
http://www.example.com/rontest