How do I parse json-object using python?

后端 未结 2 815
一个人的身影
一个人的身影 2020-12-16 01:57

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

import json

record= \'{\"shirt\":{\"red\":{\"quanitity\":100},\"blue\":{\"quantity\":10}},\"pants\":         


        
相关标签:
2条回答
  • 2020-12-16 02:35

    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)
    
    0 讨论(0)
  • 2020-12-16 02:57
    import json
    
    record = '{"shirts":{"red":{"quantity":100},"blue":{"quantity":10}},"pants":{"black":{"quantity":50}}}'
    inventory = json.loads(record)
    
    for key, value in dict.items(inventory["shirts"]):
        print key, value
    
    for key, value in dict.items(inventory["pants"]):
        print key, value
    
    0 讨论(0)
提交回复
热议问题