Python Json将数组解码为字符串

时间:2022-10-17 13:20:27

I'm trying to parse some Json, my code goes like this:

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

This above example print this :

以上示例打印此:

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

I really would like to have domainCount, RemoteIpAddress and RemoteAddress shown first and last but not least; Have a normal string list for the domainArray key. In this example, domainArray key correspond to a list that is shown like this: [[u'567gu.com', u''],

我真的想首先显示domainCount,RemoteIpAddress和RemoteAddress,但最重要的是;拥有domainArray密钥的正常字符串列表。在此示例中,domainArray键对应于如下所示的列表:[[u'567gu.com',u''],

I would like to have a list like this:

我想有一个这样的列表:

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

Any help would be greatly appreciated !

任何帮助将不胜感激 !

Thanks

2 个解决方案

#1


3  

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

#2


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))

results in

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            

#1


3  

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

#2


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))

results in

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