2

我正在尝试解析一些 Json,我的代码如下所示:

Json = '{"status":"Success", "resultsMethod":"database", "lastScrape":"2012-05-28 00:03:52", "domainCount":"45", "remoteAddress":"www.digg.com", "remoteIpAddress":"64.191.203.30", "domainArray":[["567gu.com", ""], ["64.191.203.30", ""], ["64.191.203.30.", ""], ["bellacor.com", ""], ["blogs.vancouversun.com", ""], ["da-dk.chinathin.com", ""], ["dads.new.digg.com", ""], ["deathofaclown.com", ""], ["digg-inc.com", ""], ["digg.bokefu.com", ""], ["digg.com", ""], ["digg.com.", ""], ["diggcontest.com", ""], ["diggcorp.com", ""], ["diggentertainment.com", ""], ["diggriver.com", ""], ["e.digg.com", ""], ["evercleancanada.com", ""], ["feeds.digg.com", ""], ["feeds.gawker.com", ""], ["feeds.paidcontent.org", ""], ["firthfamily.net", ""], ["m.digg.com", ""], ["media.digg.com", ""], ["new.digg.com", ""], ["rfcoron.no-ip.org", ""], ["rss.digg.com", ""], ["samtesta.com", ""], ["services.digg.com", ""], ["shopzilla.com", ""], ["theultimatebathrooms.com", ""], ["tmpnetwork.com", ""], ["www.567gu.com", ""], ["www.businesswire.com", ""], ["www.calibex.com", ""], ["www.digg-inc.com", ""], ["www.digg.com", ""], ["www.freshnews.org", ""], ["www.iconfactory.com", ""], ["www.istoleyoursite.com", ""], ["www.lethargy.org", ""], ["www.pheedcontent.com", ""], ["www.stuff.co.nz", ""], ["www.tr.im", ""], ["www.xkan.cn", ""]]}'


obj = json.loads(Json)
for key, value in obj.iteritems():
    print key,value

上面的例子打印这个:

status Success
domainArray [[u'567gu.com', u''], [u'64.191.203.30', u''], [u'64.191.203.30.', u''], [u'bellacor.com', u''], [u'blogs.vancouversun.com', u''], [u'da-dk.chinathin.com', u''], [u'dads.new.digg.com', u''], [u'deathofaclown.com', u''], [u'digg-inc.com', u''], [u'digg.bokefu.com', u''], [u'digg.com', u''], [u'digg.com.', u''], [u'diggcontest.com', u''], [u'diggcorp.com', u''], [u'diggentertainment.com', u''], [u'diggriver.com', u''], [u'e.digg.com', u''], [u'evercleancanada.com', u''], [u'feeds.digg.com', u''], [u'feeds.gawker.com', u''], [u'feeds.paidcontent.org', u''], [u'firthfamily.net', u''], [u'm.digg.com', u''], [u'media.digg.com', u''], [u'new.digg.com', u''], [u'rfcoron.no-ip.org', u''], [u'rss.digg.com', u''], [u'samtesta.com', u''], [u'services.digg.com', u''], [u'shopzilla.com', u''], [u'theultimatebathrooms.com', u''], [u'tmpnetwork.com', u''], [u'www.567gu.com', u''], [u'www.businesswire.com', u''], [u'www.calibex.com', u''], [u'www.digg-inc.com', u''], [u'www.digg.com', u''], [u'www.freshnews.org', u''], [u'www.iconfactory.com', u''], [u'www.istoleyoursite.com', u''], [u'www.lethargy.org', u''], [u'www.pheedcontent.com', u''], [u'www.stuff.co.nz', u''], [u'www.tr.im', u''], [u'www.xkan.cn', u'']]
lastScrape 2012-05-28 00:03:52
remoteAddress www.digg.com
resultsMethod database
remoteIpAddress 64.191.203.30
domainCount 45

我真的很想首先显示 domainCount、RemoteIpAddress 和 RemoteAddress,但并非最不重要;具有 domainArray 键的普通字符串列表。在此示例中,domainArray 键对应于如下所示的列表:[[u'567gu.com', u''],

我想要一个这样的列表:

DomainArray : 
ellacor.com
blogs.vancouversun.com
da-dk.chinathin.com
etc, etc.

任何帮助将不胜感激 !

谢谢

4

2 回答 2

5
obj = json.loads(Json)
print("DomainCount: %d" % obj['domainCount'])
print("DomainArray: %s" % '\n'.join(e[0] for e in obj['domainArray'])
# etc
于 2012-07-07T15:20:58.437 回答
1
try:
    import simplejson as json
except ImportError:
    import json

s = '{"status":"Success", "resultsMethod":"database", "lastScrape":"2012-05-28 00:03:52", "domainCount":"45", "remoteAddress":"www.digg.com", "remoteIpAddress":"64.191.203.30", "domainArray":[["567gu.com", ""], ["64.191.203.30", ""], ["64.191.203.30.", ""], ["bellacor.com", ""], ["blogs.vancouversun.com", ""], ["da-dk.chinathin.com", ""], ["dads.new.digg.com", ""], ["deathofaclown.com", ""], ["digg-inc.com", ""], ["digg.bokefu.com", ""], ["digg.com", ""], ["digg.com.", ""], ["diggcontest.com", ""], ["diggcorp.com", ""], ["diggentertainment.com", ""], ["diggriver.com", ""], ["e.digg.com", ""], ["evercleancanada.com", ""], ["feeds.digg.com", ""], ["feeds.gawker.com", ""], ["feeds.paidcontent.org", ""], ["firthfamily.net", ""], ["m.digg.com", ""], ["media.digg.com", ""], ["new.digg.com", ""], ["rfcoron.no-ip.org", ""], ["rss.digg.com", ""], ["samtesta.com", ""], ["services.digg.com", ""], ["shopzilla.com", ""], ["theultimatebathrooms.com", ""], ["tmpnetwork.com", ""], ["www.567gu.com", ""], ["www.businesswire.com", ""], ["www.calibex.com", ""], ["www.digg-inc.com", ""], ["www.digg.com", ""], ["www.freshnews.org", ""], ["www.iconfactory.com", ""], ["www.istoleyoursite.com", ""], ["www.lethargy.org", ""], ["www.pheedcontent.com", ""], ["www.stuff.co.nz", ""], ["www.tr.im", ""], ["www.xkan.cn", ""]]}'
obj = json.loads(s)

printorder = ['domainCount', 'remoteIpAddress', 'remoteAddress', 'lastScrape', 'resultsMethod', 'status', 'domainArray']
for var,val in ((var, obj.get(var)) for var in printorder):
    if var == 'domainArray':
        val = ''.join('\n  {}'.format(domain[0]) for domain in val)
    var = '{}:'.format(var)
    print('{:<20} {}'.format(var, val))

结果是

domainCount:         45
remoteIpAddress:     64.191.203.30
remoteAddress:       www.digg.com
lastScrape:          2012-05-28 00:03:52
resultsMethod:       database
status:              Success
domainArray:         
  567gu.com
  64.191.203.30
  64.191.203.30.
  bellacor.com
  blogs.vancouversun.com
  da-dk.chinathin.com
  dads.new.digg.com
  deathofaclown.com
  digg-inc.com
  digg.bokefu.com
  digg.com
  digg.com.
  diggcontest.com
  diggcorp.com
  diggentertainment.com
  diggriver.com
  e.digg.com
  evercleancanada.com
  feeds.digg.com
  feeds.gawker.com
  feeds.paidcontent.org
  firthfamily.net
  m.digg.com
  media.digg.com
  new.digg.com
  rfcoron.no-ip.org
  rss.digg.com
  samtesta.com
  services.digg.com
  shopzilla.com
  theultimatebathrooms.com
  tmpnetwork.com
  www.567gu.com
  www.businesswire.com
  www.calibex.com
  www.digg-inc.com
  www.digg.com
  www.freshnews.org
  www.iconfactory.com
  www.istoleyoursite.com
  www.lethargy.org
  www.pheedcontent.com
  www.stuff.co.nz
  www.tr.im
  www.xkan.cn            
于 2012-07-07T17:17:19.477 回答