无法获取JSON dict密钥

时间:2017-03-05 04:09:22

标签: python json python-3.x parsing dictionary

Create table Alok(Col1 varchar(25) NOT NULL PRIMARY KEY, Col2 BIGINT NOT NULL, Col3 nvarchar(256))
--At first, just trying to insert values for PKColumn and BIGINT Column as couldn't script for all three columns at once.
DECLARE @RowCount INT
DECLARE @RowString VARCHAR(25)
DECLARE @Random INT
DECLARE @Upper INT
DECLARE @Lower INT
DECLARE @InsertCol2 BIGINT

--To configure the items that are set once per run.
SET @Lower = 0
SET @Upper = 9223372036854775807
SET @RowCount = 0
--Setting up the RowCount
WHILE @RowCount < 5000000
BEGIN
SET @RowString = CAST(@RowCount AS VARCHAR(25))

SELECT @Random = ROUND(((@Upper - @Lower -1) * RAND() + @Lower), 0)

SET @InsertCol2 = ROUND(((@Lower + 9000000000) * RAND()), 15)

INSERT INTO Alok
    (Col1
    ,Col2)

VALUES
    (REPLICATE('0', 25 - DATALENGTH(@RowString)) + @RowString
    , @InsertCol2)

SET @RowCount = @RowCount + 1
END

我试图解析上面的这个json数据并获得&#39; zip&#39;这样做的价值

{
    "response": {
        "version": "0.1",
        "termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
        "features": {
            "conditions": 1
        }
    },

    "current_observation": {
        "image": {
            "url": "http://icons.wxug.com/graphics/wu2/logo_130x80.png",
            "title": "Weather Underground",
            "link": "http://www.wunderground.com"
        },
        "display_location": {
            "full": "San Francisco, CA",
            "city": "San Francisco",
            "state": "CA",
            "state_name": "California",
            "country": "US",
            "country_iso3166": "US",
            "zip": "94102",
        }
    }
}

然后我会收到此错误

j = json.loads(string)
keys = j.keys()
print(keys)
#current_observation

print(j['current_observation']['zip'])

所以我试图总结的是解析JSON数据并获得zip值但收效甚微。

1 个答案:

答案 0 :(得分:3)

您错过了由display_location映射的词典:

print(j['current_observation']['display_location']['zip'])