gpt4 book ai didi

javascript - 如何将 JSON 字符串对象值转换为整数?

转载 作者:行者123 更新时间:2023-11-30 19:57:35 25 4
gpt4 key购买 nike

我正在尝试将 JSON 对象值转换为整数并进行计算。

它很好地显示了 representativeRow.DTA_VAL,但是 total 的值显示了 NaN

我认为这行不通,因为其他代码运行良好。

  datav=  Number(representativeRow.DTA_VAL);

这是我的代码

  var rows = resp.Sttsapitbldata[1].row;
if (rows) {
var representativeRow;
for (i = 1; i < 30; i++) {
representativeRow = rows[i];

if(representativeRow.ITM_NM.substring(0,2)=="부산"){
// console.log(representativeRow.ITM_NM);
var sub =representativeRow.ITM_NM.substring(0,3);
var total;
var datav;
console.log(representativeRow.DTA_VAL);
datav= Number(representativeRow.DTA_VAL);

total+=datav;
console.log(total);

}

itemNm2 = representativeRow.ITM_NM;
dataV = representativeRow.DTA_VAL;
//console.log(itemNm2);
//console.log(dataV);
options.data.data.push({locname: itemNm2, listshrs: dataV});
}


korea = webponent.visual.korea.init($(".korea"), style, options);

}

请参阅下面的 JSON 文件代码。

{"Sttsapitbldata":[{"head":[{"list_total_count":88},{"RESULT":{"CODE":"INFO-000","MESSAGE":"정상 처리되었습니다."}}]},{"row":[{"STATBL_ID":"T183673021266818","DTACYCLE_CD":"YY","WRTTIME_IDTFR_ID":"2016","ITM_ID":10001,"ITM_NM":"계","CLS_ID":50033,"CLS_NM":"강간","UI_NM":"명","DTA_VAL":5155,"DTA_SVAL":null},{"STATBL_ID":"T183673021266818","DTACYCLE_CD":"YY","WRTTIME_IDTFR_ID":"2016","ITM_ID":10002,"ITM_NM":"서울","CLS_ID":50033,"CLS_NM":"강간","UI_NM":"명","DTA_VAL":1129,"DTA_SVAL":null},{"STATBL_ID":"T183673021266818","DTACYCLE_CD":"YY","WRTTIME_IDTFR_ID":"2016","ITM_ID":10003,"ITM_NM":"부산","CLS_ID":50033,"CLS_NM":"강간","UI_NM":"명","DTA_VAL":314,"DTA_SVAL":null},

最佳答案

您应该初始化 total=0。您不能在第一次迭代时添加到空值。虽然看着它,但我怀疑你想要在循环之外初始化 total

关于javascript - 如何将 JSON 字符串对象值转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53776486/

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