gpt4 book ai didi

javascript - 通过循环更改 json 格式的数据...结果增加了三倍

转载 作者:行者123 更新时间:2023-12-03 07:46:56 26 4
gpt4 key购买 nike

我需要更改 JSON 格式的数据,我想删除 dataLevel JSON 数组上第二列 y 中的引号。首先在后面的代码中,我使用此代码创建将数据表更改为 json 格式的函数

public string DataTableToJSONWithStringBuilder(DataTable table) {
var JSONString = new StringBuilder();
if (table.Rows.Count > 0) {
JSONString.Append("[");
for (int i = 0; i < table.Rows.Count; i++) {
JSONString.Append("{");
for (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();
}

我通过jquery ajax获取数据,结果是这样的:

    {"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 over 2015)"}]}"}

然后在 aspx 页面中我创建这个 javascript 函数:

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 = "";
for (var i = 0; i < DataArray.length; i++) {

JSONString += "[";

for (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;
}

这是我得到的结果的三倍.​​.....:

[{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}  ]

我想要的结果是:

[{name : 'KEBIJAKAN/PERATURAN' , y :1 }, {name : 'LAINNYA' , y :3 }, {name : 'UTAMA', y : 3}  ]

谁能帮我解决这个问题?

最佳答案

要将字符串转换为数字,只需循环数据并将该值从字符串更改为数字

var dataz = jQuery.parseJSON(dataPie.d);
dataz['dataLevel'].forEach(function(item){
item.y = +item.y || 0;
});

关于javascript - 通过循环更改 json 格式的数据...结果增加了三倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35167701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com