gpt4 book ai didi

javascript - 在表中显示 JSON 数组的值

转载 作者:行者123 更新时间:2023-12-03 12:45:33 25 4
gpt4 key购买 nike

我正在尝试显示我在表中创建的数组的值。我构建的原始解决方案是为了显示与数组中的 ID 完全匹配的数组的单个值。您可以在( http://www.users.miamioh.edu/chaudha/cse252/assignment92/ )查看此内容。输入“jjones”将显示结果。

我修改了此解决方案以接受部分查询并显示适合部分查询的每个值(注意: http://www.users.miamioh.edu/chaudha/cse252/assignment9/service/people/jjhttp://www.users.miamioh.edu/chaudha/cse252/assignment92/service/people/jj )。我开始遇到有关将数组解析到表中的问题。最终目标是显示满足部分查询的人员列表。我正在尝试找出需要在 Javascript 中更改哪些内容才能实现此目的。

我的JS如下:

$(document).ready( function () {
$("#searchClassmates").keyup(function(){
var searchValue = document.getElementById('searchClassmates').value;
$.getJSON("http://www.users.miamioh.edu/chaudha/CSE252/Assignment9/service/people/" + searchValue, function (json) {
for(var i = 0; i < json.length; i++) {
$("<tr>").appendTo("#classMateResults");
$("<td>" + json[i].firstName + "</td>").appendTo("#classMateResults");
$("<td>" + json[i].lastName + "</td>").appendTo("#classMateResults");
$("<td>" + json[i].age + "</td>").appendTo("#classMateResults");
$("<td>" + json[i].major + "</td>").appendTo("#classMateResults");
$("<td>" + json[i].phone + "</td>").appendTo("#classMateResults");
$("<td>" + json[i].email + "</td>").appendTo("#classMateResults");
$("<td>" + json[i].state + "</td>").appendTo("#classMateResults");
$("</tr>").appendTo("#classMateResults");
}
});
});
});

表:

$people = array(
'jjones' => array('firstName' => 'Jim', 'lastName' => 'Jones', 'age' => 20, 'major' => 'Computer Science', 'phone' => '212-460-9393', 'email' => 'jjones@miamioh.edu', 'state' => 'OH'),
'asmith' => array('firstName' => 'April', 'lastName' => 'Smith', 'age' => 19, 'major' => 'Mechanical Engineering', 'phone' => '913-939-3929', 'email' => 'asmith@miamioh.edu', 'state' => 'WY'),
'pstemple' => array('firstName' => 'Pat', 'lastName' => 'Stemple', 'age' => 21, 'major' => 'Theater Performance', 'phone' => '917-222-2232', 'email' => 'pstemple@miamioh.edu', 'state' => 'NY'),
'jjones1' => array('firstName' => 'Janet', 'lastName' => 'Jones', 'age' => 22, 'major' => 'Botany', 'phone' => '817-332-9392', 'email' => 'jjones1@miamioh.edu', 'state' => 'CA'),
'llerner' => array('firstName' => 'Leon', 'lastName' => 'Lerner', 'age' => 18, 'major' => 'Biology', 'phone' => '315-444-3494', 'email' => 'llerner@miamioh.edu', 'state' => 'OH'),
'mmeyer' => array('firstName' => 'Margret', 'lastName' => 'Meyer', 'age' => 24, 'major' => 'Interactive Media Studies', 'phone' => '219-333-0303', 'email' => 'mmeyer@miamioh.edu', 'state' => 'OH'),
'achaudhry' => array('firstName' => 'Anik', 'lastName' => 'Chaudhry', 'age' => 19, 'major' => 'Management Information Systems', 'phone' => '914-555-5555', 'email' => 'achaudhry@miamioh.edu', 'state' => 'NY'),
'sdogg' => array('firstName' => 'Snoop', 'lastName' => 'Dogg', 'age' => 42, 'major' => 'Botany', 'phone' => '414-333-2433', 'email' => 'sdogg@miamioh.edu', 'state' => 'CA'),
'bclinton' => array('firstName' => 'Bill', 'lastName' => 'Clinton', 'age' => 25, 'major' => 'Political Science', 'phone' => '933-440-3033', 'email' => 'bclinton@miamioh.edu', 'state' => 'AK'),);

PHP

function display_person($query) {
global $people;
$foundid = array();
foreach ($people as $k => $v)
if (stripos($k, $query) !== false)
{
$foundid[$k] = $v;
}
if(count($foundid) > 0) {
header('Content-type: application/json');
echo json_encode($foundid); // NOTE: you need to change your JS code to accept array instead of 1 person
} else {
header('HTTP/1.1 404 Not Found');
}}

最佳答案

问题是您的 json 答案的长度不正确。尝试添加一个

console.log(json.length);

在 JavaScript 循环之前。

您可以在这里找到类似问题的答案:

get size of json object

关于javascript - 在表中显示 JSON 数组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23357341/

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