2
我想以json格式序列化自定義對象,其中entryData是我的域對象的列表。事情是這樣的:Django:自定義對象json序列化
{
"total":2,
"current":1,
"entryData":[
{
"id":1,
"version":0,
"name":"Default Station"
},
{
"id":2,
"version":3,
"name":"Default Station 1"
}
]
}
在這裏我做了什麼讓JSON輸出在我的嘗試中的一種:
def ground_station_listgrid(request):
entryData = serializers.serialize("json", GroundStation.objects.all())
response_data = {}
response_data['totalPages'] = 2
response_data['currentPage'] = 1
response_data['entryData'] = entryData
return HttpResponse(json.dumps(response_data),mimetype='application/json')
但結果entryData評價爲一個字符串,用引號轉義:
{
"totalPages": 1,
"currentPage": 1,
"entryData": "[{\"pk\": 1, \"model\": \"satview.groundstation\", ....
我也tryed做這樣的事情:
def ground_station_listgrid(request):
response_data = {}
response_data['totalPages'] = 1
response_data['currentPage'] = 1
response_data['entryData'] = GroundStation.objects.all()
return HttpResponse(json.dumps(response_data),mimetype='application/json')
但我得到這個例外:[<GroundStation: nome>, <GroundStation: nome>, <GroundStation: nome>] is not JSON serializable
有人請指出我的正確方向嗎?
在此先感謝 馬爾科
非常感謝你..它的作品! – gipinani