vendredi 14 novembre 2014

python - chaîne de Django JSON à l'objet json - Stack Overflow


I am using mongodb with django and want to store json object in mongodb. Here is my code


Model


class Data(models.Model):
deviceId = models.CharField(max_length=200)
payload = models.CharField(max_length=2000)

View save data


deviceId = request.POST.get('deviceId')
payload = request.POST.get('payload')
data = Data.objects.create(deviceId=deviceId, payload=payload);
data.save()

View fetch data


data = json.dumps(list(Data.objects.all().values('deviceId','payload')))

Response


{
"data":{
"payloads":"{name:\"xyz\"}"
"id":"xxxxx"
},
}

The problem is with "payloads":"{name:\"xyz\"}". Here is a string instead of json Object.


I want "payloads":"{name:"xyz"}". How can convert this into json object in django. Is there anyway to convert all of the dataset into json object instead of iterating each object from dataset




I think you should be able to just remove the json.dumps part of the script. That's a python command to translate a valid JSON object into a string!


data = list(Data.objects.all().values('deviceId','payload'))


I am using mongodb with django and want to store json object in mongodb. Here is my code


Model


class Data(models.Model):
deviceId = models.CharField(max_length=200)
payload = models.CharField(max_length=2000)

View save data


deviceId = request.POST.get('deviceId')
payload = request.POST.get('payload')
data = Data.objects.create(deviceId=deviceId, payload=payload);
data.save()

View fetch data


data = json.dumps(list(Data.objects.all().values('deviceId','payload')))

Response


{
"data":{
"payloads":"{name:\"xyz\"}"
"id":"xxxxx"
},
}

The problem is with "payloads":"{name:\"xyz\"}". Here is a string instead of json Object.


I want "payloads":"{name:"xyz"}". How can convert this into json object in django. Is there anyway to convert all of the dataset into json object instead of iterating each object from dataset



I think you should be able to just remove the json.dumps part of the script. That's a python command to translate a valid JSON object into a string!


data = list(Data.objects.all().values('deviceId','payload'))

0 commentaires:

Enregistrer un commentaire