JSON编码返回对象数组,如何访问它们?

时间:2015-03-28 22:07:45

标签: php jquery ajax json

AJAX CALL

$.ajax({
    type: 'POST',
    url: './php/testing/notification-regrab.php',
    async: false,
    contentType: 'text/json',
    error: function (result) {
      alert("ERROR124");
    },
    success: function (result) {
      var data = $.parseJSON(result);
      console.log(data);
    }
});

结果没有json.parse

{"notes":{"0":{"id":"3","sender":"0000000011","sendee":"0000000001","sent":"2015-03-11 00:00:00","is_read":"0","type":"14","ix_msg":"You have recived a response to your group invitation!","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"Sure, I will join the project group.","target":"1","skill_list":null,"resource_list":null,"response":"1","request_type":null,"request_string":null,"rating":null},"1":{"id":"4","sender":"0000000011","sendee":"0000000001","sent":"2015-03-19 00:00:00","is_read":"0","type":"1","ix_msg":"Hey, you recieved a message from mot","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"Hey, This is a messgae sent to tom from mot","target":"0","skill_list":null,"resource_list":null,"response":null,"request_type":null,"request_string":null,"rating":null},"2":{"id":"5","sender":"0000000011","sendee":"0000000001","sent":"2015-03-19 04:13:30","is_read":"0","type":"1","ix_msg":"You have recieved a message from mot.","sender_fname":"mot","sender_lname":"mot","sender_username":"mot","msg":"Hey there friend.","target":null,"skill_list":null,"resource_list":null,"response":null,"request_type":null,"request_string":null,"rating":null}}}

结果使用json.parse(我打开了对象0的第一个结果,所以你可以看到它的含义)

Object {notes: Object}
notes: Object
  0: Object
     id: "3"
     is_read: "0"
     ix_msg: "You have recived a response to your group invitation!"
     msg: "Sure, I will join the project group."
     rating: null
     request_string: null
     request_type: null
     resource_list: null
     response: "1"
     sendee: "0000000001"
     sender: "0000000011"
     sender_fname: "mot"
     sender_lname: "mot"
     sender_username: "mot"
     sent: "2015-03-11 00:00:00"
     skill_list: null
     target: "1"
     type: "14"
     __proto__: Object
  1: Object
  2: Object
  __proto__: Object
__proto__: Object

这是构成上述数据的PHP数组:

$arr = array(
    "id" => $row["id"],
    "sender" => $row["sender"],
    "sendee" => $row["sendee"],
    "type" => $row["type"],
    "posting" => $row["posting"],
    "msg" => $row["msg"],
    "sent" => $row["sent"],
    "read" => $row["read"],
);

问题的 好的,所以我试图用jquery遍历每个通知,但我首先要测试如何从这个对象数组中返回一个通知对象......

我想做以下例子:

的console.log(数据[0] .ID);

哪个应该只返回字符串" 3",因为这是第一个通知中的第一个ID ...

如果我执行上述操作,则会显示以下错误:

Uncaught TypeError: Cannot read property 'id' of undefined

我不知道如何阅读json对象......

请有人帮助我并告诉我如何正确输出上述数据......

2 个答案:

答案 0 :(得分:1)

首先在请求中更改text / kaon with application / json也确保服务器返回正确的标题。

php部分应该是这样的:     $ note = array(" id" => 123," name" =>"某些名称")

$notes = array();
$notes[] = $note;

jQuery部分很简单:     $ .each(notes,function(i,note){     的console.log(note.name);     });

答案 1 :(得分:0)

我想通了......

我之前有过这个,但我忘了使用parseJSON ......

的console.log(data.notes [0] .ID);

由于