将嵌套的Json数据与Excel列进行比较

eufgjt7s  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(132)
"Services": {
"RoadAttributes": {
  "egoInformation": [
    {
      "roundabout": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "tollStation": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "railroadcrossing": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "speedbump": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "tunnel": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "urbanArea": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "streetClass": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "functionalRoadClass": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "constructionSite": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "laneIndexEgo": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "laneNumbers": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "source": 5
      },
      "laneAcceleration": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "source": 5
      },
      "laneDeceleration": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "value3": 5,
        "source": 5
      },
      "laneWidth": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "structuralSeperation": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "ramp": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "insideCity": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "crosswalk": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "intersection": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "source": 5
      },
      "relativeYawAngle": 5,
      "messageCounter": 4
    }
  ]
}}

字符串
JSON数据与Excel列x1c 0d1x进行比较

import json
import json as js

# Read the JSON file
with open("ampmin.json") as f:
    data = js.load(f)

# Temp_Data=data['Services']
# print(Temp_Data)
for d in data:
    Temp_Data = d['Services']


我没有尝试与上述代码,并试图读取JSON,有很多服务条目在我的JSON文件中,我想比较他们与我的Excel参数,如果匹配发现通知我们,但不知道如何乐观地检索参数,并与Excel参数比较?任何帮助将不胜感激,谢谢!

wvt8vs2t

wvt8vs2t1#

我不明白你想拿什么来比较...
.但是如果你有嵌套的JSON,那么你需要嵌套的for-循环。
在每个for-循环中,您可以使用type()检查嵌套元素中的内容-listdict-并决定是否需要使用for item in listfor key, val in dict.items()
此代码

data = {"Services": {
"RoadAttributes": {
  "egoInformation": [
    {
      "roundabout": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "tollStation": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "railroadcrossing": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "speedbump": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "tunnel": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "urbanArea": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "streetClass": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "functionalRoadClass": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "constructionSite": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "laneIndexEgo": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "laneNumbers": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "source": 5
      },
      "laneAcceleration": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "source": 5
      },
      "laneDeceleration": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "value3": 5,
        "source": 5
      },
      "laneWidth": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "structuralSeperation": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "ramp": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "insideCity": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "crosswalk": {
        "id": 5,
        "value1": 5,
        "source": 5
      },
      "intersection": {
        "id": 5,
        "value1": 5,
        "value2": 5,
        "source": 5
      },
      "relativeYawAngle": 5,
      "messageCounter": 4
    }
  ]
}}
}

for s1_key, s1_val in data['Services'].items():
    #print(s1_key, type(s1_val))
    for s2_key, s2_val in s1_val.items():
        #print(s2_key, type(s2_val))
        for s3_items in s2_val:
            #print('[0]', type(s3_items))
            for s4_key, s4_val in s3_items.items():
                #print(s4_key, type(s4_val))
                print(s1_key, s2_key, f'{s4_key:20}', s4_val)

字符串
给了我:

RoadAttributes egoInformation roundabout           {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation tollStation          {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation railroadcrossing     {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation speedbump            {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation tunnel               {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation urbanArea            {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation streetClass          {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation functionalRoadClass  {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation constructionSite     {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation laneIndexEgo         {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation laneNumbers          {'id': 5, 'value1': 5, 'value2': 5, 'source': 5}
RoadAttributes egoInformation laneAcceleration     {'id': 5, 'value1': 5, 'value2': 5, 'source': 5}
RoadAttributes egoInformation laneDeceleration     {'id': 5, 'value1': 5, 'value2': 5, 'value3': 5, 'source': 5}
RoadAttributes egoInformation laneWidth            {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation structuralSeperation {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation ramp                 {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation insideCity           {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation crosswalk            {'id': 5, 'value1': 5, 'source': 5}
RoadAttributes egoInformation intersection         {'id': 5, 'value1': 5, 'value2': 5, 'source': 5}
RoadAttributes egoInformation relativeYawAngle     5
RoadAttributes egoInformation messageCounter       4

相关问题