【问题标题】:retrieve json value python检索json值python
【发布时间】:2020-02-05 04:22:22
【问题描述】:

我有一个 JSON 值,想检索其中的每个值。

{"profile": [{"user_id": "91609430", "user_phone": "6525295", "user_type": "panger", "user_tier": "silver", "user_name": "Robert Sambuena", "user_email": "sauena.@gmail.com"}], "is_from_hc": false, "errors": [{"msg": "Driver Profile Not Found", "code": 404, "key": "profile.driver"}], "soundwaves": {"audio_messages": ["hc-education"], "access_token": "9f7fd8f060f2dc2fa", "has_context": false, "queue": "", "iso_language": "", "context": {"user_id": "", "user_tier": "", "user_name": "", "user_phone": "", "is_from_hc": false, "user_type": "", "has_context": false, "zendesk_language": "", "taxtype": "", "scenario_name": "", "zen_country": "", "booking_code": ""}}}

【问题讨论】:

标签: python json python-2.7


【解决方案1】:

您可以使用 Python 内置的 json lib 来完成此任务:

import json

json_str = '{"profile": [{"user_id": "91609430", "user_phone": "6525295", "user_type": "panger", "user_tier": "silver", "user_name": "Robert Sambuena", "user_email": "sauena.@gmail.com"}], "is_from_hc": false, "errors": [{"msg": "Driver Profile Not Found", "code": 404, "key": "profile.driver"}], "soundwaves": {"audio_messages": ["hc-education"], "access_token": "9f7fd8f060f2dc2fa", "has_context": false, "queue": "", "iso_language": "", "context": {"user_id": "", "user_tier": "", "user_name": "", "user_phone": "", "is_from_hc": false, "user_type": "", "has_context": false, "zendesk_language": "", "taxtype": "", "scenario_name": "", "zen_country": "", "booking_code": ""}}}'
json_obj = json.loads(json_str)

# Now you can iterate over the json_obj as you would do with any dict:
for k, v in json_obj.items():
    print(k, v)

【讨论】:

  • 嗨。非常感谢你的帮助。我只需要这个值 [{u'user_id': u'91609430', u'user_phone': u'658402', u'user_type': u'passenger', u'user_tier': u'silver', u'user_name ': u'Robert', u'user_email': u'sambu@gmail.com'}] 我怎样才能找回他们每个人?
  • 你有没有尝试复制并粘贴我的样本并玩它,@art?
【解决方案2】:

在 python 中使用yaml lib:(根据您的需要更新答案)

import yaml

json_str = '{"profile": [{"user_id": "91609430", "user_phone": "6525295", "user_type": "panger", "user_tier": "silver", "user_name": "Robert Sambuena", "user_email": "sauena.@gmail.com"}], "is_from_hc": false, "errors": [{"msg": "Driver Profile Not Found", "code": 404, "key": "profile.driver"}], "soundwaves": {"audio_messages": ["hc-education"], "access_token": "9f7fd8f060f2dc2fa", "has_context": false, "queue": "", "iso_language": "", "context": {"user_id": "", "user_tier": "", "user_name": "", "user_phone": "", "is_from_hc": false, "user_type": "", "has_context": false, "zendesk_language": "", "taxtype": "", "scenario_name": "", "zen_country": "", "booking_code": ""}}}'
json_obj = yaml.load(json_str)
# Retrive the Profile Value like this
profile = json_obj['profile']
print(profile)
# Iterate to get each value in profile
for i in profile:
    for j in i:
        print(j," : ", i[j])

【讨论】:

    猜你喜欢
    • 2022-06-13
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    相关资源
    最近更新 更多