Python:解析JSON文件问题

时间:2016-02-15 10:43:32

标签: python json

我正在尝试解析JSON文件,其中ID为关键字,响应标头为values。我正在尝试获取ID及其特定value(服务器详细信息) 。    #1.JSON

{
    "1": {
        "content-length": "51828",
        "expires": "Fri, 19 Feb 2016 02:38:36 GMT",
        "x-powered-by": "W3 Total Cache/0.9.4.1",
        "accept-ranges": "bytes",
        "strict-transport-security": "max-age=31536000",
        "vary": "Accept-Encoding,Cookie",
        "server": "Apache/2.2.22 (Ubuntu)",
        "last-modified": "Fri, 12 Feb 2016 02:38:36 GMT",
        "etag": "\"ca74-52b89905bc300\"",
        "pragma": "public",
        "cache-control": "max-age=600, private, must-revalidate, public",
        "date": "Mon, 15 Feb 2016 10:06:58 GMT",
        "x-frame-options": "SAMEORIGIN",
        "content-type": "text/html; charset=UTF-8"
    },
    "2": {
        "content-length": "0",
        "content-language": "en",
        "x-powered-by": "Servlet/2.5 JSP/2.1",
        "set-cookie": "JSESSIONID=hdkyWBjGT5YD0GSG9wMRSGVybnlLvhvwKHMc0PJdxN8JbKclygWQ!919258543!229342577; path=/; HttpOnly",
        "x-oracle-dms-ecid": "549485340858380",
        "server": "BigIP",
        "connection": "close",
        "location": "http://www.oracle.com/us/corporate/acquisitions/bigmachines/index.html",
        "date": "Mon, 15 Feb 2016 10:07:02 GMT",
        "access-control-allow-origin": "*",
        "content-type": "text/html; charset=utf-8",
        "x-frame-options": "SAMEORIGIN"
    }
}

我试过了,

#Check.py
import json

with open('json_data.json') as json_data:
    data = json.load(json_data)
    print data

for key,value in data.iteritems():
    if 'server' in value:
        for appid,headers in value['server'].iteritems():
            print appid,","headers

我收到以下错误,

表示appid,标题值为'[server']。iteritems(): AttributeError:'unicode'对象没有属性'iteritems'

Expected Result :  

1,Apache/2.2.22 (Ubuntu)
2,BigIP

不确定我为什么会遇到这个问题?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

第二部分应该是:

for key,value in data.iteritems() :
    if 'server' in value:
        print key+","+value['server']