javascript - change json formated data by looping... the result is tripled -
i need change json formated data , want remove quotes in second column y on datalevel json array. first in code behind make function change datatable json format code
public string datatabletojsonwithstringbuilder(datatable table) { var jsonstring = new stringbuilder(); if (table.rows.count > 0) { jsonstring.append("["); (int = 0; < table.rows.count; i++) { jsonstring.append("{"); (int j = 0; j < table.columns.count; j++) { if (j < table.columns.count - 1) { jsonstring.append("\"" + table.columns[j].columnname.tostring() + "\":" + "\"" + table.rows[i][j].tostring() + "\","); } else if (j == table.columns.count - 1) { jsonstring.append("\"" + table.columns[j].columnname.tostring() + "\":" + "\"" + table.rows[i][j].tostring() + "\""); } } if (i == table.rows.count - 1) { jsonstring.append("}"); } else { jsonstring.append("},"); } } jsonstring.append("]"); } return jsonstring.tostring(); }
i got data through jquery ajax , result this:
{"d":"{ "datalevel":[{"name":"kebijakan/peraturan","y":"1"}, {"name":"lainnya","y":"3"},{"name":"utama","y":"3"}], "datastatus" :[{"level":"kebijakan/peraturan","status":"belum dimulai","level1":"kebijakan/peraturan","y":"1","proyek":"pjm16.02.utama pke sop penjaminan"},{"level":"lainnya","status":"belum dimulai","level1":"lainnya","y":"3","proyek":"skl16.02 menyusun blueprint kegiatan sosial kemasyarakatan (csr) lps |skl16.03.utama seminar/sosialisasi/fgd dengan lembaga tertentu (termasuk seminar hut lps, seminar/sosialisasi/fgd dengan melibatkan anggota fkssk) |skl16.04 peningkatan hubungan kelembagaan, khususnya lps-bi-ojk"},{"level":"utama","status":"belum dimulai","level1":"utama","y":"1","proyek":"skl16.01.utama sosialisasi program penjaminan kepada masyarakat"},{"level":"utama","status":"active","level1":"utama","y":"2","proyek":"gai16.01.utama menyusun pedoman audit kinerja |pso16.01.utama penyusunan kajian organisasi lps (cary on 2015)"}]}"}
then in aspx page make javascript function :
function getdata(datapie, tahun) { var dataz = jquery.parsejson(datapie.d); var dataarray = dataz['datalevel']; // var data = json.stringify(dataarray); var countcolumn = countinobject(dataarray); console.log(dataarray); var jsonstring = ""; (var = 0; < dataarray.length; i++) { jsonstring += "["; (var j = 0; j < countcolumn; j++) { jsonstring += "{"; if (j < countcolumn - 1) { jsonstring += "name : '" + dataarray[j].name + "' , "; jsonstring += "y :" + dataarray[j].y + " "; jsonstring += "}, "; } else if (j == countcolumn - 1) { jsonstring += "name : '" + dataarray[j].name + "', "; jsonstring += "y : " + dataarray[j].y + "} "; } } if (i == dataarray.length - 1) { jsonstring += " "; } else { jsonstring += "}, "; } jsonstring += "]"; }; return jsonstring; }
this result got tripled...:
[{name : 'kebijakan/peraturan' , y :1 }, {name : 'lainnya' , y :3 }, {name : 'utama', y : 3} }, ][{name : 'kebijakan/peraturan' , y :1 }, {name : 'lainnya' , y :3 }, {name : 'utama', y : 3} }, ][{name : 'kebijakan/peraturan' , y :1 }, {name : 'lainnya' , y :3 }, {name : 'utama', y : 3} ]
the result want :
[{name : 'kebijakan/peraturan' , y :1 }, {name : 'lainnya' , y :3 }, {name : 'utama', y : 3} ]
anyone can me how fix this?
to convert strings numbers loop on data , change value string number
var dataz = jquery.parsejson(datapie.d); dataz['datalevel'].foreach(function(item){ item.y = +item.y || 0; });
Comments
Post a Comment