使用python编码和迭代json但使用多个根元素

时间:2017-08-23 23:08:32

标签: python json parsing encoding

大家晚上好, 我有python脚本,它将列表的一些数据写入json文件。脚本遍历列表并写入json文件,它有效但我有一个问题:根元素也被迭代,它在json文件中被写入数千次,因此它变得无效。 这是json应该是这样的一部分:

{
   "acList": [{
      "Icao": "400025",
      "Alt": 24475,
      "GAlt": 24475,
      "AltT": 0,
      "Call": "TEST1234",
      "Tisb": false,
      "TrkH": false,
      "Sqk": "0644",
      "Help": false,
      "VsiT": 0,
      "Gnd": false,
      "SpdTyp": 0,
      "CallSus": false,
      "Trt": 1
  },
  {
     "Icao": "49528A",
     "Alt": 17375,
     "GAlt": 17632,
     "InHg": 30.177166,
     "AltT": 0,
     "Call": "TAP362",
     "Lat": 50.813118,
     "Long": -1.117325,
     "Mlat": false,
     "Tisb": false,
     "Spd": 357.0,
     "Trak": 39.9,
     "TrkH": false,
     "Sqk": "7473",
     "Help": false,
     "Vsi": -1472,
     "VsiT": 0,
     "Gnd": false,
     "SpdTyp": 0,
     "CallSus": false,
     "Trt": 2
  }]
} 

虽然这是我得到的:

{
    "acList": [
      {
     "GAlt": 33975,
     "Mlat": false,
     "Tisb": false,
     "Trt": 1,
     "Help": false,
     "InHg": 0,
     "CallSus": false,
     "Long": 17.1752,
     "TrkH": false,
     "Icao": "02A196",
     "Sqk": "3710",
     "Call": "TU217",
     "Trak": 261,
     "Gnd": false,
     "Lat": 37.8024,
     "Vsi": 0,
     "Alt": 33975,
     "AltT": 0,
     "Spd": 428,
     "SpdTyp": 0,
     "VsiT": 0
   }
 ]
  }{
     "acList": [
      {
     "GAlt": 0,
     "Mlat": false,
     "Tisb": false,
     "Trt": 1,
     "Help": false,
     "InHg": 0,
     "CallSus": false,
     "Long": 14.4933,
     "TrkH": false,
     "Icao": "4D2027",
     "Sqk": "2704",
     "Call": "KM103",
     "Trak": 113,
     "Gnd": false,
     "Lat": 35.8478,
     "Vsi": 0,
     "Alt": 0,
     "AltT": 0,
     "Spd": 0,
     "SpdTyp": 0,
     "VsiT": 0
   }
 ]
 }{

正如你所看到的那样,重复并重复了根元素。我应该实现的只是保留第一个根元素和正确的标点符号。这是我在Python中的实际代码:

count = 0
for hex in keys:
    data = {
        "acList": [{
        "Icao": icao[count],
        "Alt": alt[count],
        "GAlt": alt[count],
        "InHg": 0,
        "AltT": 0,
        "Call": call[count],
        "Lat": lat[count],
        "Long": long[count],
        "Mlat": False,
        "Tisb": False,
        "Spd": speed[count],
        "Trak": track[count],
        "TrkH": False,
        "Sqk": sqk[count],
        "Help": False,
        "Vsi": vsi[count],
        "VsiT": 0,
        "Gnd": False,
        "SpdTyp": 0,
        "CallSus": False,
        "Trt": count + 1
        }]
     }
   count = count + 1
   jsonData = json.dumps(data, indent=2, separators=(',', ': '))
   print(jsonData)
   with open('aircraft.json', 'a') as f:
      f.write(jsonData)

对于非常长的帖子抱歉,我希望你能帮助我。谢谢!

1 个答案:

答案 0 :(得分:1)

正如评论所说,你的循环逻辑是错误的。要按预期输出JSON字符串,您的目标data应该是data=dict(acList=[object ...])之类的结构。对于你的情况,你甚至不需要循环,列表理解就足够了:

data = dict(acList=[
    {
    "Icao": icao[count],
    "Alt": alt[count],
    "GAlt": alt[count],
    "InHg": 0,
    "AltT": 0,
    "Call": call[count],
    "Lat": lat[count],
    "Long": long[count],
    "Mlat": False,
    "Tisb": False,
    "Spd": speed[count],
    "Trak": track[count],
    "TrkH": False,
    ....
    } for count in len(keys)]
)

感谢。