gpt4 book ai didi

javascript - 如何从 JSON 列表中获取特定数据

转载 作者:行者123 更新时间:2023-11-30 08:44:15 24 4
gpt4 key购买 nike

我有这个 JSON

{
"doctors": [
{
"id": 8,
"schedules": [
{
"id": 8,
"totime": "11:17",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "10:17",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "d1",
"degree": "DA(Anaesthesia)",
"email": "1@2.com",
"imagePath": "",
"department": "Bio-Chemistry",
"name": "d1",
"userid": 51,
"gender": "Male",
"mobile": "1234567900"
},
{
"id": 10,
"schedules": [
{
"id": 10,
"totime": "12:35",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "11:35",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "d3",
"degree": "BDS",
"email": "d3@d3.com",
"imagePath": "",
"department": "Bio-Chemistry",
"name": "d3",
"userid": 56,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 1,
"schedules": [
{
"id": 1,
"totime": "12:55",
"dayId": 1,
"location": "Somajiguda",
"fromtime": "11:55",
"hospitalId": 5,
"day": "Monday",
"hospital": "Yashoda"
}
],
"username": "doctor",
"degree": "BDS",
"email": "",
"imagePath": null,
"department": "Critical Care",
"name": "doctor",
"userid": 4,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 7,
"schedules": [
{
"id": 7,
"totime": "11:17",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "11:17",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "donald",
"degree": "DA(Anaesthesia)",
"email": "donald@doctor.com",
"imagePath": "",
"department": "Bio-Chemistry",
"name": "donald",
"userid": 47,
"gender": "Male",
"mobile": "1234567989"
},
{
"id": 6,
"schedules": [
{
"id": 6,
"totime": "11:15",
"dayId": 1,
"location": "Somajiguda",
"fromtime": "11:15",
"hospitalId": 5,
"day": "Monday",
"hospital": "Yashoda"
}
],
"username": "john",
"degree": "BDS",
"email": "john@john.com",
"imagePath": null,
"department": "Anesthesiology",
"name": "john",
"userid": 46,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 5,
"schedules": [
{
"id": 5,
"totime": "13:11",
"dayId": 2,
"location": "Somajiguda",
"fromtime": "12:11",
"hospitalId": 5,
"day": "Tuesday",
"hospital": "Yashoda"
}
],
"username": "sknayak",
"degree": "BDS",
"email": "sknayak@sknayak.com",
"imagePath": "",
"department": "Anesthesiology",
"name": "sknayak",
"userid": 38,
"gender": "Male",
"mobile": "1234567890"
},
{
"id": 2,
"schedules": [
{
"id": 2,
"totime": "16:26",
"dayId": 6,
"location": "Somajiguda",
"fromtime": "15:26",
"hospitalId": 5,
"day": "Saturday",
"hospital": "Yashoda"
}
],
"username": "drsukant",
"degree": "BDS",
"email": "",
"imagePath": null,
"department": "Anesthesiology",
"name": "sukant",
"userid": 9,
"gender": "Male",
"mobile": "1234567890"
}
]
}

在这个 JSON 中有一个唯一的字段 id。我通过这样的 ajax 获取这个 json

var test=$.ajax({  
type: "GET",
url: projectUrl+"getDoctors",
dataType:"json",
jsonp: true,
async:false
}).responseText;
console.log(test);

正如您在 JSON 中看到的那样,有一个字段 id。例如,对于 id=8,用户名是 d1,id=10,用户名是 d3。我将 id 存储在 session 中。例如,如果 id 是 8,则我只想要那些 ID 为 8 的详细信息(用户名 d1,电子邮件 1@2.com.....)。

那么如何将JSON过滤到特定的值。

最佳答案

您可以创建一个 computed对于特定项目:

self.doctors = ko.observableArray();
self.d3Doctor = ko.computed(function() {
return ko.utils.arrayFirst(self.doctors(), function(obj) {
return obj.id === 8;
});
});

现在你只需要担心填充 doctors observableArray:

$.getJSON(projectUrl+"getDoctors", function(response) {
ko.utils.arrayPushAll(yourViewModel.doctors, response.doctors);
});

这允许进行以下操作:

<div data-bind="if: d3Doctor()">
<h3>Doctor with ID 8</h3>
<p>Name: <span data-bind="text: d3Doctor().name"></span></p>
<p>E-mail: <span data-bind="text: d3Doctor().email"></span></p>
<p>Address: <span data-bind="text: d3Doctor().address"></span></p>
...
</div>

关于javascript - 如何从 JSON 列表中获取特定数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23336067/

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