I am sending information between client and Django server, and I would like to use JSON to this. I am sending simple information - list of strings. I tried using django.core.serializers
, but when I did, I got
AttributeError: 'str' object has no attribute '_meta'
It seems this can be used only for Django objects. How can I serialize simple, Python objects?
Json.NET has excellent support for serializing and deserializing collections of objects. To serialize a collection - a generic list, array, dictionary, or your own custom collection - simply call the serializer with the object you want to get JSON for.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
You can use pure Python to do it:
import json list = [1, 2, (3, 4)] # Note that the 3rd element is a tuple (3, 4) json.dumps(list) # '[1, 2, [3, 4]]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With