2009-07-18 56 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