遍历 JSON 对象 [英] Iterating through a JSON object

查看:32
本文介绍了遍历 JSON 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遍历 JSON 对象以导入数据,即标题和链接.我似乎无法访问 : 之后的内容.

JSON:

<预><代码>[{"title": "宝贝 (Feat. Ludacris) - 贾斯汀比伯","description": "Baby (Feat. Ludacris) by Justin Bieber on Grooveshark","link": "http://listen.grooveshark.com/s/Baby+Feat+Ludacris+/2Bqvdq","pubDate": "2010 年 4 月 28 日星期三 02:37:53 -0400",pubTime":1272436673,"TinyLink": "http://tinysong.com/d3wI","SongID": "24447862","SongName": "宝贝 (Feat. Ludacris)","ArtistID": "1118876","ArtistName": "贾斯汀比伯","专辑ID": "4104002","专辑名称": "我的世界 (第二部分); http://tinysong.com/gQsw","LongLink": "11578982","GroovesharkLink": "11578982",链接":http://tinysong.com/d3wI"},{"title": "Feel Good Inc - Gorillaz","description": "Feel Good Inc by Gorillaz on Grooveshark","link": "http://listen.grooveshark.com/s/Feel+Good+Inc/1UksmI","pubDate": "2010 年 4 月 28 日星期三 02:25:30 -0400",pubTime":1272435930}]

我尝试使用字典:

def getLastSong(user,limit):base_url = 'http://gsuser.com/lastSong/'user_url = base_url + str(user) + '/' + str(limit) + "/"原始 = urllib.urlopen(user_url)json_raw=raw.readlines()json_object = json.loads(json_raw[0])#filtering 并使其看起来不错.gsongs = []打印 json_object对于 json_object[0] 中的歌曲:印刷歌曲

这段代码只打印:之前的信息.(忽略 Justin Bieber 的曲目 :))

解决方案

您对 JSON 数据的加载有点脆弱.而不是:

json_raw= raw.readlines()json_object = json.loads(json_raw[0])

你真的应该这样做:

json_object = json.load(raw)

你不应该认为你得到的是JSON 对象".你拥有的是一个清单.该列表包含两个字典.dicts 包含各种键/值对,所有字符串.当您执行 json_object[0] 时,您要求的是列表中的第一个 dict.当您迭代它时,使用 for song in json_object[0]:,您将迭代 dict 的键.因为这就是迭代 dict 时得到的结果.如果要访问与该 dict 中的键关联的值,可以使用例如 json_object[0][song].

这些都不是特定于 JSON 的.它只是基本的 Python 类型,它们的基本操作在任何教程中都有介绍.

I am trying to iterate through a JSON object to import data, i.e. title and link. I can't seem to get to the content that is past the :.

JSON:

[
    {
        "title": "Baby (Feat. Ludacris) - Justin Bieber",
        "description": "Baby (Feat. Ludacris) by Justin Bieber on Grooveshark",
        "link": "http://listen.grooveshark.com/s/Baby+Feat+Ludacris+/2Bqvdq",
        "pubDate": "Wed, 28 Apr 2010 02:37:53 -0400",
        "pubTime": 1272436673,
        "TinyLink": "http://tinysong.com/d3wI",
        "SongID": "24447862",
        "SongName": "Baby (Feat. Ludacris)",
        "ArtistID": "1118876",
        "ArtistName": "Justin Bieber",
        "AlbumID": "4104002",
        "AlbumName": "My World (Part II);
http://tinysong.com/gQsw",
        "LongLink": "11578982",
        "GroovesharkLink": "11578982",
        "Link": "http://tinysong.com/d3wI"
    },
    {
        "title": "Feel Good Inc - Gorillaz",
        "description": "Feel Good Inc by Gorillaz on Grooveshark",
        "link": "http://listen.grooveshark.com/s/Feel+Good+Inc/1UksmI",
        "pubDate": "Wed, 28 Apr 2010 02:25:30 -0400",
        "pubTime": 1272435930
    }
]

I tried using a dictionary:

def getLastSong(user,limit):
    base_url = 'http://gsuser.com/lastSong/'
    user_url = base_url + str(user) + '/' + str(limit) + "/"
    raw = urllib.urlopen(user_url)
    json_raw= raw.readlines()
    json_object = json.loads(json_raw[0])

    #filtering and making it look good.
    gsongs = []
    print json_object
    for song in json_object[0]:   
        print song

This code only prints the information before :. (ignore the Justin Bieber track :))

解决方案

Your loading of the JSON data is a little fragile. Instead of:

json_raw= raw.readlines()
json_object = json.loads(json_raw[0])

you should really just do:

json_object = json.load(raw)

You shouldn't think of what you get as a "JSON object". What you have is a list. The list contains two dicts. The dicts contain various key/value pairs, all strings. When you do json_object[0], you're asking for the first dict in the list. When you iterate over that, with for song in json_object[0]:, you iterate over the keys of the dict. Because that's what you get when you iterate over the dict. If you want to access the value associated with the key in that dict, you would use, for example, json_object[0][song].

None of this is specific to JSON. It's just basic Python types, with their basic operations as covered in any tutorial.

这篇关于遍历 JSON 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆