对于带有json数据的javascript中的每个循环 [英] for each loop in javascript with json data

查看:88
本文介绍了对于带有json数据的javascript中的每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,它是一个宁静的json API.

I have the following code, which is a restful json api.

//  Program guide Rest

$(document).ready(function() {
  $.ajax({
    cache: false,
      url: "http://engridmedia.com/next/api/epg/scheduler/id/200",
    type: 'GET',
    crossDomain: true,
    dataType: 'json',
    success: function() {
        alert("EPG Success");
    },
    error: function() {
        alert('EPG Failed!');
    },
})

.then(function(data) {
    for (var i = 0; i < 6; ++i)
    {
        var prefix = (i == 0 ? "" : i.toString());

        $('.programtitle' + prefix).append(data[i].program_title);
        $('.ch-start' + prefix).append(data[i].start_time);
        $('.ch-end' + prefix).append(data[i].end_time);
        $('.ch-programdesc' + prefix).append(data[i].desc);


    }
});

我想使用我的phonegap html中的数据.这意味着我将不得不通过多次调用每个数据,因为我将在数组中存储20多个数据,有时更少. javascript中的每个循环都可以轻松地做到这一点,就像在php中从数据库中回显数据一样?

I want to use the data in my phonegap html . that means i would have to call each data by doing this a bunch of times since i will have over 20 data in the array and sometimes less. is there a for each loop in javascript to do this easily like echoing data from the database in php?

 <?php foreach ($query as $row)
{ ;?>
  <h3><?php echo $row -> program_title; ?> | <?php echo $row -> start_time; ?> | <?php echo $row -> end_time; ?></h3>
  <div>
    <p>
   <?php echo $row -> desc; ?>
    </p>
  </div>
   <?php } ?>

推荐答案

正如ja408所说,您应该在javascript中使用此语法:

As ja408 sayed you should use this syntax in javascript :

for(var k in yourJson) {
   console.log(k, result[k]);
}

谢谢

这篇关于对于带有json数据的javascript中的每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆