How to extract the attributes from a node in a json dictionary?

前端 未结 1 412
深忆病人
深忆病人 2021-01-29 15:26

I have a dictionary which contains the following json elements.

myjsonDictionary = \\
{
  \"Teams\": {
    \"TeamA\": {
      \"@oid\": \"123.0.0.1\",
      \"d         


        
1条回答
  •  北海茫月
    2021-01-29 15:45

    With recursive traversal for specific keys:

    def get_team_idlvel_oid_pair(d, search_key):
        for k, v in d.items():
            if k.startswith('Team'):
                if k == search_key:
                    return '{}{}.{}'.format(d['@oid'] + '.' if '@oid' in d else '',
                                            v['@oid'], v['dataRequestList']['state']['@oid'])
                elif any(k.startswith('Team') for k_ in v):
                    return get_team_idlvel_oid_pair(v, search_key)
    
    
    print(get_team_idlvel_oid_pair(myjsonDictionary['Teams'], 'TeamA'))
    print(get_team_idlvel_oid_pair(myjsonDictionary['Teams'], 'TeamSub'))
    

    Sample output:

    123.0.0.1.2
    123.0.0.1.3.2
    

    0 讨论(0)
提交回复
热议问题