遍历JSON数组 [英] looping through JSON array

查看:94
本文介绍了遍历JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了另一个问题,用户立即指出了我正确的方向.

I have recently posted another question which straight away users pointed me in the right direction.

$.ajax({
  type: 'POST',
  url: './',
  data: 'token=' + token + '&re=8',
  cache: false,
  timeout: 5000,
  success: function(html) {
    auth(html);
    var JSON_array = eval(html);
    alert(JSON_array[0].username);
  }
});

这可以正确返回数据,但是我想执行一种"foreach".该数组包含有关多个传入和传出即时消息的数据.因此,如果一个用户一次与一个以上的人交谈,我需要遍历.数组的结构如下.

this returns the data correctly but I want to perform a kind of 'foreach'. the array contains data about multiple incoming and outgoing Instant Messages. So if a user is talking to more than one person at a time i need to loop through. the array's structure is as follows.

Array(
  [0] => Array
    (
      [username] => Emmalene
      [contents] => 
        <ul><li class="name">ACTwebDesigns</li><li class="speech">helllllllo</li></ul>
        <ul><li class="name">ACTwebDesigns</li><li class="speech">sds</li></ul>
        <ul><li class="name">ACTwebDesigns</li><li class="speech">Sponge</li><li class="speech">dick</li></ul>
        <ul><li class="name">ACTwebDesigns</li><li class="speech">arghh</li></ul>
    )
)

非常感谢任何帮助.

推荐答案

由于您已经使用jQuery,因此可以使用

Well since you are using jQuery already you could use the each function:

$.ajax({ 
        type: 'POST', url: './', data: 'token=' + token + '&re=8', cache: false, timeout: 5000,
        success: function(html){ 
            auth(html);
            var JSON_array = eval(html);
            $.each(JSON_array, function(index, data) {
                $('someelement').append(data.contents);
            });
        }                           
    });

这篇关于遍历JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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