[問題] Javascript json to array 轉換

看板Web_Design作者 (5245566)時間9年前 (2015/01/29 21:38), 9年前編輯推噓4(406)
留言10則, 4人參與, 最新討論串1/1
var test; $.post("query.php",{id:id}) .done(function(data){ test = $.parseJSON(data);}); console.log(test); data是有撈出資料的 但是test一直顯示是undefined 這個問題是什麼呢!? data內容: [{"0":"120.95","rest_lon":"120.95","1":"24.8","rest_lat":"24.8", "2":"\u65b0\u7af9\u5e02\u5927\u5b78\u8def1001\u865f", "rest_address":"\u65b0\u7af9\u5e02\u5927\u5b78\u8def1001\u865f", "3":"03-3369880","rest_tel":"03-3369880"}, {"0":"120.96","rest_lon":"120.96","1":"24.81","rest_lat":"24.81", "2":"\u65b0\u7af9\u7e23\u5927\u5b78\u8def1\u865f", "rest_address":"\u65b0\u7af9\u7e23\u5927\u5b78\u8def1\u865f", "3":"03-2571864","rest_tel":"03-2571864"}] -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.113.134.94 ※ 文章網址: https://www.ptt.cc/bbs/Web_Design/M.1422538725.A.37D.html

01/29 21:58, , 1F
把data的內容一起po出來吧
01/29 21:58, 1F

01/29 22:08, , 2F
這個應該不用parse可以直接拿來用吧
01/29 22:08, 2F

01/29 22:09, , 3F
至少我直接貼到console結果是object不是string
01/29 22:09, 3F

01/29 22:11, , 4F
看起來沒錯耶,應該直接把data拿去用不用再parseJSON了吧
01/29 22:11, 4F
var test; $.post("query.php",{id: id}) .done(function(data){ test = data;}); alert(test); 還是一樣test是undefined 主要就是想要把撈出來的資料可以到括號外面使用 ※ 編輯: tony5566 (140.113.134.94), 01/29/2015 22:27:34

01/29 22:33, , 5F
jQuery.extend()?
01/29 22:33, 5F

01/29 22:46, , 6F

01/29 23:15, , 7F
小的淺見...妳在alert(test)的時候,post還沒把data抓
01/29 23:15, 7F

01/29 23:16, , 8F
回來。 非同步概念? 有錯煩請糾正....
01/29 23:16, 8F

01/29 23:36, , 9F
我猜樓上是對的XD
01/29 23:36, 9F

01/30 00:07, , 10F
恩恩 對XD 感謝提醒
01/30 00:07, 10F
文章代碼(AID): #1KoZVbDz (Web_Design)