Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing list to JSON

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?

like image 644
gruszczy Avatar asked Jan 27 '10 14:01

gruszczy


People also ask

Can you serialize a list in JSON?

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.

What is serializing in JSON?

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).

How do I deserialize JSON?

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.


1 Answers

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]]' 
like image 184
Emil Ivanov Avatar answered Sep 22 '22 02:09

Emil Ivanov