gpt4 book ai didi

javascript - 如何在codeigniter中获取json对象的值

转载 作者:行者123 更新时间:2023-11-28 20:38:47 25 4
gpt4 key购买 nike

我正在使用 codeigniter 框架来开发我的网站。目前我正在使用 jQuery 发送 AJAX 请求以从服务器获取数据。我尝试了两种方法,一种将数据放在 PHP 关联数组中,另一种将数据放在 JSON 对象中。请查看我从 AJAX 请求返回的数据:

{
"o": [
{
"q_id": "83",
"t_id": "4",
"question": "jjjjs.jfdaskldjf",
"option1": "jjjjasdfasdf",
"option2": "jjj",
"option3": "lll",
"option4": "lll",
"answer": "lll",
"marks": "22"
},
{
"q_id": "84",
"t_id": "4",
"question": "This is testing",
"option1": "2",
"option2": "7",
"option3": "8",
"option4": "9",
"answer": "2",
"marks": "2"
},
{
"q_id": "85",
"t_id": "4",
"question": "hello this is another test",
"option1": "a",
"option2": "b",
"option3": "c",
"option4": "d",
"answer": "a",
"marks": "2"
},
{
"q_id": "86",
"t_id": "4",
"question": "another test",
"option1": "8",
"option2": "9",
"option3": "0",
"option4": "1",
"answer": "1",
"marks": "2"
},
{
"q_id": "87",
"t_id": "4",
"question": "last question ",
"option1": "z",
"option2": "x",
"option3": "c",
"option4": "v",
"answer": "c",
"marks": "2"
}
]
}

我的访问方式是:

alert(data["o"][0]);

但是出现的错误是:

Uncaught TypeError: Cannot read property '0' of undefined

任何人都可以给我一些关于如何获取值或将对象存储在数组中的建议。

最佳答案

因为这里是一个对象而不是数组..使用 .

试试这个

alert(data.o[0])

使用循环$.each函数获取o内部对象的值

data.o[0].each(function(i,v){
console.log(v.q_id);
});

关于javascript - 如何在codeigniter中获取json对象的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14790502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com