python - AttributeError: 'str' object has no attribute 'loads', json.loads() -


snippets

    import json     teststr = '{"user": { "user_id": 2131, "name": "john", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}'     json = json.load(teststr) 

throws exception

traceback (most recent call last):   file "<input>", line 1, in <module> attributeerror: 'str' object has no attribute 'loads' 

how solve problem?

json.load takes in file pointer, , you're passing in string. meant use json.loads takes in string first parameter.

secondly, when import json, should take care not overwrite it, unless it's intentional: json = json.load(teststr) <-- bad. overrides module have imported, making future calls module function calls dict created.

to fix this, can use variable once loaded:

import json teststr = '{"user": { "user_id": 2131, "name": "john", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}' jsonstr = json.loads(teststr) 

or can change module name you're importing

import json json teststr = '{"user": { "user_id": 2131, "name": "john", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}' json = json.loads(teststr) 

or can import functions want use module

from json import loads teststr = '{"user": { "user_id": 2131, "name": "john", "gender": 0,  "thumb_url": "sd", "money": 23, "cash": 2, "material": 5}}' json = loads(teststr) 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -