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

时间:2014-04-28 08:26:11

标签: javascript jquery json knockout.js

我有这个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.So例如,如果id为8则我只想要那些ID为8的详细信息(用户名d1,电子邮件1@2.com .....)。

所以如何将JSON过滤为特定值。

4 个答案:

答案 0 :(得分:2)

您可以为特定项目创建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>

答案 1 :(得分:1)

您可以使用each找到它,试试这个:

$(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;
            }
        });
    });
});

答案 2 :(得分:1)

可能是这个?:

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);

答案 3 :(得分:1)

是。我可以回答这个问题。

在我的方式中,我个人更喜欢将所有数据转换为数组并对其进行迭代或操作。

正如本question所述,我们已经将数据推送到这种格式:

doctors.push({id:currPat.id,name:currPat.username});

所以,现在我可以使用filter数组的数组doctors函数:

var result = doctors.filter(function(currentObject) {
    return currentObject.id === 8;
});

console.log(result); // {id: 8, name:d1}

或者您也可以在JQuery中使用.map(),只需使用该功能检查data.id并返回您想要的对。

如果你愿意争取nano秒的表现。我猜我的方法比.map()