简体   繁体   中英

Getting Django object “is not JSON serializable”

I have a webservice that reads a JSON object and it was giving me unicodeEncodeError exception. After googling a little bit, I saw How can I convert a dict to a unicode JSON string?

(I followed other questions that were related to unicodeEncodeError but I was still getting AttributeError: 'dict' object has no attribute 'content')

I did what was mentioned in that particular question and now I am getting ..... is not JSON serializable

Can anyone tell me what do I have to do now?

Following is my code:

def someMethod()
    some_data = request.data
    json_string1 = json.dumps(some_data)           #GETTING ERROR ON THIS LINE
    json_string2 = get_string(json_string1)
    archive = call.send_json(json_string2)


def get_string(value):
    find_unicode = re.compile('\\\\u([\da-f]{4})')

    def get_parsed_unicode(x):
        return chr(int(x.group(1), 16))
    return find_unicode.sub(get_parsed_unicode, str(value))

Thanks for the help !!

When you're passing the object to the method, use foo.serealize(true) . Include JQUERY to use this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM