使用JSON数据的python api调用

时间:2014-06-29 09:42:48

标签: python api

Python API调用的新手我在下面有JSON数据示例

我可以获取数据调用,例如(swell.*)(swell.minBreakingHeight),并且无需担心会返回所有swell数据。工作请求也好,

我似乎无法通过成功案例well.primary.height缩小范围 显然,此处的格式不正确,并且不断返回[]

我如何获得额外的水平?

[{
timestamp: 1366902000,
localTimestamp: 1366902000,
issueTimestamp: 1366848000,
fadedRating: 0,
solidRating: 0,
swell: {
    minBreakingHeight: 1,
    absMinBreakingHeight: 1.06,
    maxBreakingHeight: 2,
    absMaxBreakingHeight: 1.66,
    unit: "ft",
    components: {
         combined: {
         height: 1.1,
         period: 14,
         direction: 93.25,
         compassDirection: "W"
    },
    primary: {
         height: 1,
         period: 7,
         direction: 83.37,
         compassDirection: "W"
    },

1 个答案:

答案 0 :(得分:0)

使用您的数据代码:

data = [{
'timestamp': 1366902000,
'localTimestamp': 1366902000,
'issueTimestamp': 1366848000,
'fadedRating': 0,
'solidRating': 0,
'swell': {
    'minBreakingHeight': 1,
    'absMinBreakingHeight': 1.06,
    'maxBreakingHeight': 2,
    'absMaxBreakingHeight': 1.66,
    'unit': "ft",
    'components': {
         'combined': {
         'height': 1.1,
         'period': 14,
         'direction': 93.25,
         'compassDirection': "W"
    },
    'primary': {
         'height': 1,
         'period': 7,
         'direction': 83.37,
         'compassDirection': "W"
    }
}
}
}
]

In [54]: data[0]['timestamp']
Out[54]: 1366902000

In [55]: data[0]['swell']['components']['primary']['height']
Out[55]: 1

所以使用点符号,你应该打电话:

swell.components.primary.height

有关解析json文件的最佳见解,请参阅此另一个stackoverflow question