2009-07-18 57 views
2

我想顯示一個json文件的變量(日期),但它看起來沒有工作。我究竟做錯了什麼?通過jQuery顯示json變量

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data) 
     { 
      $.each(data.headers, function(i,item) 
      { 
       if(i < 2) 
       { 
       $("body").append("+item.Date+"); 
       } 
      }); 

     });   
    }); 

</script> 

回答

0

你應該能夠理解日期格式不會被解析並.getJSON(客戶機上的分析器)將無法解析它。

0

$ .getJSON在url,data,callback順序中有3個參數。因此,在這種情況下,你會怎麼做:

$(document).ready(function(){ 
$.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", null, function(data){ 
    $.each(data.headers, function(i,item){ 
    if(i < 2){ 
    $("body").append("+item.Date+"); 
    } 
    }); 
}); 
}); 
+0

它應該與兩個 – 2009-07-18 14:07:50

1

不僅僅是谷歌的JavaScript +傾倒,你會發現PHP的print_r的對JavaScript的一些等價物。

這是一個例子(從http://www.openjs.com/scripts/others

/dump_function_php_print_r.php) 
/** 
* Function : dump() 
* Arguments: The data - array,hash(associative array),object 
* The level - OPTIONAL 
* Returns : The textual representation of the array. 
* This function was inspired by the print_r function of PHP. 
* This will accept some data as the argument and return a 
* text that will be a more readable version of the 
* array/hash/object that is given. 
* Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php 
*/ 
function dump(arr,level) { 
    var dumped_text = ""; 
    if(!level) level = 0; 

    //The padding given at the beginning of the line. 
    var level_padding = ""; 
    for(var j=0;j<level+1;j++) level_padding += " "; 

    if(typeof(arr) == 'object') { //Array/Hashes/Objects 
     for(var item in arr) { 
      var value = arr[item]; 

      if(typeof(value) == 'object') { //If it is an array, 
       dumped_text += level_padding + "'" + item + "' ...\n"; 
       dumped_text += dump(value,level+1); 
      } else { 
       dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n"; 
      } 
     } 
    } else { //Stings/Chars/Numbers etc. 
     dumped_text = "===>"+arr+"<===("+typeof(arr)+")"; 
    } 
    return dumped_text; 
} 
+0

工作在for..in循環內添加一個hasOwnProperty檢查,這很好。我能夠使用它與jquery.terminal輸出json對象到調試工具的控制檯。 – 2011-06-06 21:59:53

0

是您的客戶端發送的日期? 因爲有時我發現,而不是發送字符串,它呈現爲JavaScript的日期(這是時間戳)

,所以你需要這樣的轉換:

var yourDate = new Date(item.Date); 

的完整代碼:

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     $.getJSON("http://json-head.appspot.com/?url=http://www.trinum.com/ibox/chatel/images/photofull.jpg&callback=?", function(data) 
     { 
      $.each(data.headers, function(i,item) 
      { 
       if(i < 2) 
       { 
       $("body").append(new Date(item.Date)); 
       } 
      }); 

     });   
    }); 

</script> 
4

實際的問題是你的JSON響應結構,headers只是一個對象,並且使用$.each函數,你正在遍歷這個對象成員(Content-Length,Via等...),如果headers用來存儲多個對象,你應該就可以使用數組符號「[]」:

{ 
    "status_code": 200, 
    "ok": true, 
    "headers": [{ 
     "Content-Length": "7068", 
     "Via": "HTTP\/1.1 GWA", 
     "X-Powered-By": "ASP.NET", 
     "Accept-Ranges": "bytes", 
     "X-Google-Cache-Control": "remote-fetch", 
     "Server": "Microsoft-IIS\/6.0", 
     "Last-Modified": "Tue, 06 Feb 2007 07:57:38 GMT", 
     "ETag": "\"8b5f5c78c449c71:2c6a\"", 
     "Cache-Control": "no-cache", 
     "Date": "Sun, 19 Jul 2009 05:51:42 GMT", 
     "Content-Type": "image\/jpeg" 
    }] 
}; 

通過這樣做,$.each將迭代通過headers陣列中定義的所有對象。

陣列符號開始[左托架)中,用]右支架)結束,並且該值與,逗號)分離:

JSON Array Notation http://www.json.org/array.gif

對於有關JSON語法和結構檢查的更多信息this site