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

Javascript 如何从JSON列表中获取特定数据,javascript,jquery,json,knockout.js,Javascript,Jquery,Json,Knockout.js,我有这个JSON { "doctors": [ { "id": 8, "schedules": [ { "id": 8, "totime": "11:17", "dayId": 2, "location": "Somajiguda", "fromtime": "10:17", "hospitalId": 5,

我有这个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是唯一的

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存储在会话中。因此,例如,如果id是8,那么我只需要那些详细信息,用户名是d1,电子邮件1@2.com..... 他的身份证是8

因此,如何将JSON过滤为特定值。

您可以使用每个值来查找它,请尝试以下操作:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: projectUrl+"getDoctors",
        dataType: "json",
        jsonp: true,
        async: false
    }).done(function(data) {
        $.each(data.doctors, function(i, v) {
            if (v.id == '8') {
                console.log('found', v.username, v.email);
                return false;
            }
        });
    });
});
您可以使用每个选项来查找它,请尝试以下操作:

$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: projectUrl+"getDoctors",
        dataType: "json",
        jsonp: true,
        async: false
    }).done(function(data) {
        $.each(data.doctors, function(i, v) {
            if (v.id == '8') {
                console.log('found', v.username, v.email);
                return false;
            }
        });
    });
});
可能是这个吗

function findById(data, id){
     for(var i=0;i<data.length;i++){
          if(data[i].id === id) return { username:data[i].username, email:data[i].email};
     }
     // Or, you can use $.grep like this:
     // var foundDoctors = $.grep(data,function(e){return e.id === id;})
     // return foundDoctors.length > 0 && foundDoctors[0] || null;

     // Or, you can use filter() method from Array (IE 9+)
}

findById(jsondata["doctors"], 8);
可能是这个吗

function findById(data, id){
     for(var i=0;i<data.length;i++){
          if(data[i].id === id) return { username:data[i].username, email:data[i].email};
     }
     // Or, you can use $.grep like this:
     // var foundDoctors = $.grep(data,function(e){return e.id === id;})
     // return foundDoctors.length > 0 && foundDoctors[0] || null;

     // Or, you can use filter() method from Array (IE 9+)
}

findById(jsondata["doctors"], 8);

对。我可以回答这个问题

以我的方式,我个人更喜欢将所有数据转换成数组,并生成或操作它

如本文所述,我们已经将数据推送到以下格式:

doctors.push({id:currPat.id,name:currPat.username});
因此,现在我可以使用阵列过滤器函数来处理阵列:

或者也可以在JQuery中使用.map,只需使用函数检查data.id并返回所需的对


如果你想争取纳秒级的表现。我想我的方法比.map好是的。我可以回答这个问题

以我的方式,我个人更喜欢将所有数据转换成数组,并生成或操作它

如本文所述,我们已经将数据推送到以下格式:

doctors.push({id:currPat.id,name:currPat.username});
因此,现在我可以使用阵列过滤器函数来处理阵列:

或者也可以在JQuery中使用.map,只需使用函数检查data.id并返回所需的对

如果你想争取纳秒级的表现。我想我的方法比.map好。您可以为特定项目创建一个:

self.doctors = ko.observableArray();
self.d3Doctor = ko.computed(function() {
    return ko.utils.arrayFirst(self.doctors(), function(obj) {
        return obj.id === 8;
    });
});
现在你只需要担心医生们的人数:

$.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>
您可以为特定项目创建一个:

self.doctors = ko.observableArray();
self.d3Doctor = ko.computed(function() {
    return ko.utils.arrayFirst(self.doctors(), function(obj) {
        return obj.id === 8;
    });
});
现在你只需要担心医生们的人数:

$.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>

感谢您的回答,+1我将尝试您的方式感谢您的回答,+1我将尝试您的方式感谢您的回答+1,我正在从ajax获取JSON,因此如何在您获得jsondata中的数据后记录所有findById方法。findById将返回一个对象,其中包含相关医生的用户名和电子邮件。我的意思是,在ajax的成功部分,我应该调用?+1以获得成功或完成的帮助。Manoj另一个答案实际上表明了这一点谢谢你回答+1,我从ajax获得了JSON,那么在获得jsondata中的数据之后,如何记录所有findById方法。findById将返回一个对象,其中包含相关医生的用户名和电子邮件。我的意思是,在ajax的成功部分,我应该调用?+1以获得成功或完成的帮助。Manoj另一个答案实际上展示了你能用knockout.js回答这个问题吗你能用knockout.js回答这个问题吗谢谢你的回答,+1。你能告诉我d3Doctor是什么吗?d3Doctor是id为8的物品的名称。在html中,您现在可以执行以下操作:ID为8的医生:当然,请参见我的编辑。d3Doctor是id为8的项目,因此您可以使用该项目的所有属性。在这种情况下,您仍然需要ajax调用来填充医生列表,感谢您回答+1。你能告诉我d3Doctor是什么吗?d3Doctor是id为8的物品的名称。在html中,您现在可以执行以下操作:ID为8的医生:当然,请参见我的编辑。d3Doctor是id为8的项目,因此您可以使用该项目的所有属性