Python Json将数组解码为字符串

时间:2012-07-07 15:15:26

标签: python arrays json string

我正在尝试解析一些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.

非常感谢任何帮助!

由于

2 个答案:

答案 0 :(得分:3)

obj = json.loads(Json)
print("DomainCount: %d" % obj['domainCount'])
print("DomainArray: %s" % '\n'.join(e[0] for e in obj['domainArray'])
# etc

答案 1 :(得分: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