Displaying better error message than “No JSON object could be decoded”

后端 未结 11 1065
既然无缘
既然无缘 2020-11-30 23:05

Python code to load data from some long complicated JSON file:

with open(filename, \"r\") as f:
  data = json.loads(f.read())

(note: the be

11条回答
  •  一生所求
    2020-11-30 23:53

    As to me, my json file is very large, when use common json in python it gets the above error.

    After install simplejson by sudo pip install simplejson.

    And then I solved it.

    import json
    import simplejson
    
    
    def test_parse_json():
        f_path = '/home/hello/_data.json'
        with open(f_path) as f:
            # j_data = json.load(f)      # ValueError: No JSON object could be decoded
            j_data = simplejson.load(f)  # right
        lst_img = j_data['images']['image']
        print lst_img[0]
    
    
    if __name__ == '__main__':
        test_parse_json()
    

提交回复
热议问题