dongneng5383 2016-08-02 15:32
浏览 170
已采纳

如何处理包含数字字符串的JSON

I am pulling JSON data from a webserver using the following PHP code

 $result = mysql_query("select lat,lng from gpsdata limit 10");
 $rows = array();
 while($r = mysql_fetch_assoc($result)) {
  $rows[] = $r;
 }

 print json_encode($rows);

I am using Javascript to get this data with this

 $.getJSON('returngps.php').done(function(data) {
    for (var i=0;i< data.length;i++ ){
    console.log(data[i]);
    }
  }

My issue is the data I am getting returned. The output I am currently getting is:

   {lat: "53.399793333333", lng: "-6.3844516666667"}

What I want to work with is:

   {lat: 53.399793333333, lng: -6.3844516666667}

Is there a way to convert this?

  • 写回答

2条回答 默认 最新

  • douou5933 2016-08-02 15:37
    关注

    Try using parseFloat:

    data[i].lat = parseFloat(data[i].lat);
    data[i].lng = parseFloat(data[i].lng);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?