通过jquery显示json变量

时间:2009-07-18 13:55:33

标签: jquery ajax json

我想要显示一个json文件的变量(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("+item.Date+");
              }
           });

         });        
    });

</script>

5 个答案:

答案 0 :(得分: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

有关JSON语法和结构检查的更多信息this site

答案 1 :(得分:1)

只需谷歌搜索javascript + dump,你就会发现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;
}

答案 2 :(得分:0)

您应该在客户端上有一个解析器,它可以理解日期格式,而日期不会被解析,而.getJSON()也不会为您解析它。

答案 3 :(得分: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+");
   }
  });
});
});

答案 4 :(得分:0)

您的客户端是否按日期发送? 因为有时我发现它不是像String那样发送,而是呈现为javascript Date(它是时间戳)

所以你需要像这样转换:

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>