我有一个生成的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 ''
当我运行这个脚本时,我会以可读的格式显示我的数据,并且它就在那里 - 现在我只想阅读" dist"在" StrikeData"并且条件只列出40以下的距离。这是我挣扎的地方。对于我的生活,我甚至无法通过python来打印" dist" key infact我无法打印任何位于" StrikeData"
但是,我可以阅读上面的其他按键" StrikeData"例如" StationLon" " StationLat"等
任何帮助都将不胜感激。
答案 0 :(得分:0)
距离应在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中的数据都不符合此条件,因此不会打印任何内容。