如何使用python解析json-object? [英] How do I parse json-object using python?

查看:73
本文介绍了如何使用python解析json-object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析json对象并遇到问题.

I am trying to parse a json object and having problems.

import json

record= '{"shirt":{"red":{"quanitity":100},"blue":{"quantity":10}},"pants":{"black":{"quantity":50}}}'
inventory = json.loads(record)
#HELP NEEDED HERE
for item in inventory:
    print item

我可以弄清楚如何获得这些值.我可以拿钥匙.请帮忙.

I can figure out how to obtain the values. I can get keys. Please help.

推荐答案

您不再拥有JSON对象,而拥有Python

You no longer have a JSON object, you have a Python dictionary. Iterating over a dictionary produces its keys.

>>> for k in {'foo': 42, 'bar': None}:
...   print k
... 
foo
bar

如果您要访问这些值,请为原始词典建立索引或使用返回不同内容的方法之一.

If you want to access the values then either index the original dictionary or use one of the methods that returns something different.

>>> for k in {'foo': 42, 'bar': None}.iteritems():
...   print k
... 
('foo', 42)
('bar', None)

这篇关于如何使用python解析json-object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆