change json formated data by looping... the result is tripled

时间:2016-02-03 02:57:09

标签: javascript c# json

i need to change JSON formated data , i want to remove the quotes in the second column y on the dataLevel JSON Array. First in code behind i make function to change datatable to json format with this code

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

i got the data through jquery ajax and the result is like 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 over 2015)"}]}"}

then in the aspx page i make this 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 = "";
  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;
}

this is the result i got is 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 i want is :

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

anyone can help me how to fix this?

1 个答案:

答案 0 :(得分:1)

To convert the strings to numbers just loop over the data and change that value from string to number

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