简体   繁体   中英

Finding Data in JSON with python

I'm so I've the value of the ID "8" and don't but I want to get the User value XXX-XXX of the Json. Is there a way to get the value? Thank you, if you can help.

   users:{
   "XXX-XXX":{
      "Info":{
         "ID":"8",
         "Created": "2021-07-10",
         "Plan": "Basic"}},
   "DDD-DDD":{
       "Info":{
          "ID":"10",
          "Created": "2021-07-11",
          "Plan": "Prime"}}
      }
    }

You can do this naively using pure python and iterating:

def get_user_value(id_):
    for key, user in users.items():
        if user['info']['ID'] == id_:
            return key
    return "Not-Found"

If your data is large, however, you may want to explore more optimized approaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM