簡體   English   中英

Python解碼JSON

[英]Python decoding JSON

我有一個生成的JSON文件,我試圖編寫一個python腳本,在其中可以讀取JSON文件,一旦滿足條件,就可以在PI上激活例程。 但是,我在讀取python中的JSON文件以獲取所需數據時遇到了麻煩。

JSON文件如下所示:

{
    "TimestampEpoch": 1514337528024,
    "StationLat": -26.358342,
    "StationLon": 120.620019,
    "SoftwareVersion": "1.9.0.8120",
    "DetectorModel": "LD-350",
    "AntennaAlignment": 0,
    "StrikeCount": 55,
    "StormCount": 0,
    "StrikeData": [{
        "time": 29639,
        "millis": 1514333639000,
        "dist": 439,
        "bng": 332.70000000000005,
        "lat": -22.823276496341727,
        "lon": 118.65980333825893,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 29975,
        "millis": 1514333975000,
        "dist": 436,
        "bng": 339.5,
        "lat": -22.6637997412206,
        "lon": 119.13533272287572,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30051,
        "millis": 1514334051000,
        "dist": 529,
        "bng": 336.5,
        "lat": -21.964135129972036,
        "lon": 118.57970482593052,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30053,
        "millis": 1514334053000,
        "dist": 548,
        "bng": 341.90000000000003,
        "lat": -21.646237895040606,
        "lon": 118.97714953741881,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30074,
        "millis": 1514334074000,
        "dist": 666,
        "bng": 358.3,
        "lat": -20.347305744621128,
        "lon": 120.43114211954078,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 30091,
        "millis": 1514334091000,
        "dist": 451,
        "bng": 338.3,
        "lat": -22.5661850362272,
        "lon": 118.9997657520629,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30213,
        "millis": 1514334213000,
        "dist": 445,
        "bng": 340.70000000000005,
        "lat": -22.559589456858671,
        "lon": 119.19103156558813,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30338,
        "millis": 1514334338000,
        "dist": 527,
        "bng": 342.90000000000003,
        "lat": -21.802954792173065,
        "lon": 119.12298209450069,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30395,
        "millis": 1514334395000,
        "dist": 517,
        "bng": 344.70000000000005,
        "lat": -21.850074765420533,
        "lon": 119.30160949216324,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30411,
        "millis": 1514334411000,
        "dist": 439,
        "bng": 335.70000000000005,
        "lat": -22.735547655819584,
        "lon": 118.86244538684973,
        "type": 1,
        "polarity": 1,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30465,
        "millis": 1514334465000,
        "dist": 435,
        "bng": 340.70000000000005,
        "lat": -22.645118167469189,
        "lon": 119.22223589812695,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30474,
        "millis": 1514334474000,
        "dist": 426,
        "bng": 338.6,
        "lat": -22.769695914648562,
        "lon": 119.10739796460815,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30517,
        "millis": 1514334517000,
        "dist": 422,
        "bng": 343.20000000000005,
        "lat": -22.706276839927302,
        "lon": 119.4336531844265,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30550,
        "millis": 1514334550000,
        "dist": 502,
        "bng": 341.3,
        "lat": -22.057012959363277,
        "lon": 119.0621863925286,
        "type": 1,
        "polarity": 0,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30568,
        "millis": 1514334568000,
        "dist": 626,
        "bng": 0.70000000000000007,
        "lat": -20.706484302115673,
        "lon": 120.69331598591792,
        "type": 1,
        "polarity": 0,
        "compoundType": 2,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 30619,
        "millis": 1514334619000,
        "dist": 429,
        "bng": 341.90000000000003,
        "lat": -22.671184630446479,
        "lon": 119.32400123250297,
        "type": 1,
        "polarity": 1,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30740,
        "millis": 1514334740000,
        "dist": 482,
        "bng": 334.1,
        "lat": -22.429482567996327,
        "lon": 118.57644968246939,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30802,
        "millis": 1514334802000,
        "dist": 509,
        "bng": 343.40000000000003,
        "lat": -21.947598119246557,
        "lon": 119.21367843405182,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 30932,
        "millis": 1514334932000,
        "dist": 501,
        "bng": 357.8,
        "lat": -21.838265205726479,
        "lon": 120.43417373312407,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 30987,
        "millis": 1514334987000,
        "dist": 504,
        "bng": 352.40000000000003,
        "lat": -21.846516520942281,
        "lon": 119.97585623802213,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31061,
        "millis": 1514335061000,
        "dist": 532,
        "bng": 339.6,
        "lat": -21.845403176401145,
        "lon": 118.82793102850755,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31095,
        "millis": 1514335095000,
        "dist": 476,
        "bng": 338.40000000000003,
        "lat": -22.35275623240533,
        "lon": 118.92019795058822,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31096,
        "millis": 1514335096000,
        "dist": 461,
        "bng": 340.8,
        "lat": -22.420408704787498,
        "lon": 119.14858320666184,
        "type": 1,
        "polarity": 1,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31125,
        "millis": 1514335125000,
        "dist": 543,
        "bng": 333.70000000000005,
        "lat": -21.944786037307939,
        "lon": 118.29322928909902,
        "type": 1,
        "polarity": 0,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31151,
        "millis": 1514335151000,
        "dist": 456,
        "bng": 353.70000000000005,
        "lat": -22.265499884683305,
        "lon": 120.13493890271367,
        "type": 1,
        "polarity": 1,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31163,
        "millis": 1514335163000,
        "dist": 554,
        "bng": 345.3,
        "lat": -21.513872273875773,
        "lon": 119.26477299490306,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31187,
        "millis": 1514335187000,
        "dist": 461,
        "bng": 334.5,
        "lat": -22.589028884800324,
        "lon": 118.69130961855463,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31190,
        "millis": 1514335190000,
        "dist": 461,
        "bng": 345.6,
        "lat": -22.322829802559983,
        "lon": 119.5081430161772,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31211,
        "millis": 1514335211000,
        "dist": 450,
        "bng": 341,
        "lat": -22.510056831175177,
        "lon": 119.19713605692327,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31221,
        "millis": 1514335221000,
        "dist": 529,
        "bng": 347.3,
        "lat": -21.694740285215946,
        "lon": 119.49738343680414,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31236,
        "millis": 1514335236000,
        "dist": 543,
        "bng": 349.70000000000005,
        "lat": -21.531761761814693,
        "lon": 119.68393479671597,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31250,
        "millis": 1514335250000,
        "dist": 533,
        "bng": 348.5,
        "lat": -21.639083078219766,
        "lon": 119.59467921262058,
        "type": 1,
        "polarity": 0,
        "compoundType": 2,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31339,
        "millis": 1514335339000,
        "dist": 526,
        "bng": 357.70000000000005,
        "lat": -21.612952163347568,
        "lon": 120.4163746302582,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 31344,
        "millis": 1514335344000,
        "dist": 526,
        "bng": 335,
        "lat": -22.038453902384596,
        "lon": 118.46863480066909,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31344,
        "millis": 1514335344000,
        "dist": 539,
        "bng": 347.1,
        "lat": -21.610104095695242,
        "lon": 119.45917399508555,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31391,
        "millis": 1514335391000,
        "dist": 536,
        "bng": 332.1,
        "lat": -22.061375971933707,
        "lon": 118.19224364927328,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31402,
        "millis": 1514335402000,
        "dist": 543,
        "bng": 348.8,
        "lat": -21.545588955773745,
        "lon": 119.60303835252884,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31508,
        "millis": 1514335508000,
        "dist": 460,
        "bng": 342.70000000000005,
        "lat": -22.387187628113903,
        "lon": 119.29270963231856,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 31920,
        "millis": 1514335920000,
        "dist": 547,
        "bng": 344,
        "lat": -21.603736403856253,
        "lon": 119.16555944440349,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 32050,
        "millis": 1514336050000,
        "dist": 558,
        "bng": 343.6,
        "lat": -21.517402635433793,
        "lon": 119.10118425525948,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 32071,
        "millis": 1514336071000,
        "dist": 563,
        "bng": 342.8,
        "lat": -21.493685086667679,
        "lon": 119.01530105404373,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 32072,
        "millis": 1514336072000,
        "dist": 560,
        "bng": 344.5,
        "lat": -21.479020325404111,
        "lon": 119.17768587022923,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 32089,
        "millis": 1514336089000,
        "dist": 471,
        "bng": 335.90000000000003,
        "lat": -22.464740290545972,
        "lon": 118.75276302335249,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 32101,
        "millis": 1514336101000,
        "dist": 481,
        "bng": 302,
        "lat": -24.001746592394504,
        "lon": 116.61161165846737,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 32312,
        "millis": 1514336312000,
        "dist": 514,
        "bng": 295.8,
        "lat": -24.266914385503728,
        "lon": 116.06272864500153,
        "type": 1,
        "polarity": 0,
        "compoundType": 2,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 32328,
        "millis": 1514336328000,
        "dist": 736,
        "bng": 2.8000000000000003,
        "lat": -19.72000669424304,
        "lon": 120.96222291627785,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 32427,
        "millis": 1514336427000,
        "dist": 515,
        "bng": 295,
        "lat": -24.320304717886881,
        "lon": 116.02149565519926,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 32439,
        "millis": 1514336439000,
        "dist": 569,
        "bng": 335.8,
        "lat": -21.655160674908917,
        "lon": 118.36906830000049,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": true,
        "NMEAChecksum": 255
    }, {
        "time": 32478,
        "millis": 1514336478000,
        "dist": 532,
        "bng": 297.3,
        "lat": -24.080840483434571,
        "lon": 115.97134537670488,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 32791,
        "millis": 1514336791000,
        "dist": 477,
        "bng": 305.7,
        "lat": -23.795622841783853,
        "lon": 116.81980755479187,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 33090,
        "millis": 1514337090000,
        "dist": 669,
        "bng": 347.70000000000005,
        "lat": -20.450062973111823,
        "lon": 119.2565952488157,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 33099,
        "millis": 1514337099000,
        "dist": 525,
        "bng": 350.8,
        "lat": -21.67704264192249,
        "lon": 119.80987551646857,
        "type": 1,
        "polarity": 1,
        "compoundType": 2,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 33187,
        "millis": 1514337187000,
        "dist": 501,
        "bng": 338.3,
        "lat": -22.144634501719153,
        "lon": 118.82584090050709,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 33259,
        "millis": 1514337259000,
        "dist": 500,
        "bng": 303.90000000000003,
        "lat": -23.783521377777497,
        "lon": 116.54893498720863,
        "type": 0,
        "polarity": 1,
        "compoundType": 1,
        "correlated": false,
        "NMEAChecksum": 255
    }, {
        "time": 33476,
        "millis": 1514337476000,
        "dist": 524,
        "bng": 297.8,
        "lat": -24.080259854117323,
        "lon": 116.06210416089792,
        "type": 0,
        "polarity": 0,
        "compoundType": 0,
        "correlated": false,
        "NMEAChecksum": 255
    }],
    "StormData": []
}

我的Python腳本是

import urllib 
import json
f = urllib.urlopen("http://mysite/lightningdata/ngxarchive.json")
values = json.load(f)
f.close()

for StrikeData in values['StrikeData']:
for key, value in StrikeData.iteritems():
    print key, 'is:', value
print ''

當我運行此腳本時,我將數據以一種可讀的格式進行布局,並且一切都已存在-現在,我只想讀取“ StrikeData”下的“ dist”,並且有一個條件只能列出小於40的距離。我苦苦掙扎。 對於我一生,我什至無法獲取python來實際上僅打印“ dist”鍵,而我也無法使其打印任何位於“ StrikeData”下的鍵

但是,我可以閱讀“ StrikeData”上方的其他鍵,例如“ StationLon”,“ StationLat”等。

任何援助將不勝感激。

距離應在StrikeData['dist']

for StrikeData in values['StrikeData']:
    print 'Distance is: ', StrikeData['dist']

演示

如果只想顯示40以下的距離,則為:

for StrikeData in values['StrikeData']:
    if StrikeData['dist'] < 40:
        print 'Distance is: ', StrikeData['dist']

但是示例JSON中的數據均不滿足此條件,因此不會打印任何內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM