How to parse data in JSON format?

后端 未结 5 1658
梦谈多话
梦谈多话 2020-11-21 07:12

My project is currently receiving a JSON message in python which I need to get bits of information out of. For the purposes of this, let\'s set it to some simple JSON in a s

5条回答
  •  攒了一身酷
    2020-11-21 07:51

    For URL or file, use json.load(). For string with .json content, use json.loads().

    #! /usr/bin/python
    
    import json
    # from pprint import pprint
    
    json_file = 'my_cube.json'
    cube = '1'
    
    with open(json_file) as json_data:
        data = json.load(json_data)
    
    # pprint(data)
    
    print "Dimension: ", data['cubes'][cube]['dim']
    print "Measures:  ", data['cubes'][cube]['meas']
    

提交回复
热议问题