在python中计算JSON对象

时间:2012-12-11 22:00:14

标签: python json object count

我想计算结果JSON对象的数量:我该怎么办?

我有这个功能:(问题出在“for i in r:”)

import urllib
import json
from classes import ricerca_IMDB

def search_IMDB (titolo):
    sito='http://www.omdbapi.com/?s='+titolo

    r=0
    r=json.loads(urllib.urlopen(sito).read())

    i=0
    filmList = []
    try:
        for i in r:
            filmList.append(ricerca_IMDB(r['Search'][i]['Title'], r['Search'][i]['Year'], r['Search'][i]['imdbID']))
    except:
        filmList = []

    return filmList

1 个答案:

答案 0 :(得分:2)

完全搞砸了你的类型处理。迭代序列yields elements

for i in r['Search']:
  filmList.append(ricerca_IMDB(i['Title'], i['Year'], i['imdbID']))