1
如何使JavaScript中的變量生成並從json數組中給它賦值?將json數組寫入javascript變量
該陣列是由PHP腳本發送,看起來像這樣:
php
$stat = array("v1" => "$v1", "v2" => "$v2", "v3" => "$v3",
"v4" => "$v4", "v5" => "$v5", "pump" => "$pump", "flow" => "$flow");
echo json_encode(($stat));
html/javascript
$.ajaxSetup({ cache: false });
setInterval(function(){
$.getJSON('statusdata.php',function(data) {
$.each(data, function(key, val) {
// I try to do something like this..
var v1 = key[1];
var v2 = key[2];
and so on..
然後我想用一個變量與某種警告,提醒的彈出窗口。
成才,如:
if (v1 == 1){
run the popup function!
}
誰能幫助我?
看來你不知道如何訪問對象的屬性/數組元素。我建議閱讀MDN JavaScript指南:https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects。也許你也必須閱讀'$ .each'是如何工作的:http://api.jquery.com/jQuery.each/。僅供參考,'data'將是一個對象,所以'key'將是該屬性的名稱(例如''v1'',''pump''),'val'將是該屬性的值。 – 2013-04-07 17:45:18