Я хочу преобразовать python dict из dict в "JSON объекты", но у меня есть целочисленные ключи в моем dict и в целочисленных значениях тоже.
Вот почему я получаю ошибку " TypeError: ключи должны быть строкой"
также одна вещь заключается в том, что одиночные кавычки иногда вызывают проблемы
Что я хочу это:
Просто JSON объект в моей ajax функции успеха, чтобы я отображал его в моем шаблоне, используя итерацию JSON.
здесь я предоставляю свой код:
urls.py:
urlpatterns = [
...,
path('get_match_group_specs_selected_data/', views.get_group_data_using_ajax, name = 'ajax_call')
]
views.py:
def get_group_data_using_ajax(request):
group_id = request.POST['group_id']
// I'm doing operation here for DB querys which results a dict what i am providing below
//group_dict_records = dict_return(group_dict_queryset)
match_group_specs_dict_data = group_dict_records
print(match_group_specs_dict_data)
app_json = json.dumps(match_group_specs_dict_data)
return HttpResponse(app_json)
результат group_dict_records:
{'Academics': {'IB': {'IB_4th':
{'id': 8, 'group': 1, 'group__group_group': 'Academics-CBSE/IB-upto5th', 'group__name': 'French',
'category__type': 'Academics', 'board__name': 'IB', 'stream__name': None, 'tuition_class__name': '4th',
'subject__name': 'French', 'streams': [None], 'subjects': {8: 'French'}},
'IB_5th': {'id': 10, 'group': 1, 'group__group_group': 'Academics-CBSE/IB-upto5th', 'group__name': 'French',
'category__type': 'Academics', 'board__name': 'IB', 'stream__name': None, 'tuition_class__name': '5th',
'subject__name': 'French', 'streams': [None], 'subjects': {10: 'French'}},
'IB_Pre-Primary': {'id': 2337, 'group': 1, 'group__group_group': 'Academics-CBSE/IB-upto5th', 'group__name': 'French',
'category__type': 'Academics', 'board__name': 'IB', 'stream__name': None, 'tuition_class__name': 'Pre-Primary',
'subject__name': 'French', 'streams': [None], 'subjects': {2337: 'French'}}}}}
my_template. html:
if(selected_group_id != 0){
$.ajax({
url : "../get_match_group_specs_selected_data/",
type : "POST",
data : {
group_id : selected_group_id,
csrfmiddlewaretoken : '{{ csrf_token }}'
},
success:function(response){
alert('S');
$("#ajax_response").html(response);
var json_data = JSON.parse(response);
myFun(response);
},
error:function(response){
alert('E');
$("#ajax_response").html(response.responseText);
}
});
}
// The ajax call will render the data on same page as the field chalges in my_template
myFun(response){
...
i'll iterate the JSON object here.
}