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

时间:2018-12-14 09:07:35

标签: javascript json

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

它很好地显示了representativeRow.DTA_VAL,但总数显示了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},

1 个答案:

答案 0 :(得分:1)

您应该初始化total = 0。您不能在第一次迭代中将其添加为空值。虽然看了一下,但我怀疑您想在循环外初始化总计