[英]django view pass json to javascript
I try to pass json to javascript.我尝试将 json 传递给 javascript。 I want make json like this我想像这样制作 json
data: [
{
value: 335,
name: 'Coding'
}, {
value: 310,
name: 'Database'
}, {
value: 234,
name: 'UIX'
}, {
value: 135,
name: 'Manajemen'
},]
}]
I want make json like this, so I can put to javascript我想像这样制作 json,所以我可以放入 javascript
{
value: 335,
name: 'Coding'
},
And this is my code这是我的代码
cursor.execute('SELECT u.nama_ktgu, COUNT(m.nim) as jml FROM mahasiswa_mhs_si m, mata_kuliah_kategori_utama u, mata_kuliah_kategori k WHERE k.kode_ktgu_id = u.id AND m.kode_ktg_id = k.id GROUP BY k.id')
ktg_mhs = cursor.fetchall()
#JSON CHART
chart_ktg = {}
for i in ktg_mhs:
chart_ktg['value'] = ktg_mhs[i][1]
chart_ktg['name'] = ktg_mhs[i][0]
json = json.dumps(chart_ktg)
use this:用这个:
import json
ktg_mhs = (
(1,2),
(3,4),
(5,6),
)
#JSON CHART
result = []
for i in ktg_mhs:
chart_ktg = {
'value': i[1],
'name': i[0]
}
result.append(chart_ktg)
json = json.dumps(result)
print(json)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.