2014-04-28 26 views
1

名單得到具體的數據我有這樣的JSON如何從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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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": "[email protected]", 
     "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是unique.I我通過Ajax獲得此JSON這樣

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

正如可以在JSON看到有id.For例如用於ID = 8的用戶名的字段爲D1,ID = 10,用戶名是d3.I正在存儲該ID在session.So例如,當ID是8然後我想只有那些細節(用戶名D1,電子郵件[email protected] .....),其ID爲8

那麼如何過濾JSON到一個特定的值。

回答

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> 
+0

感謝您的回答,+ 1。你能告訴我什麼是d3Doctor? – SpringLearner

+0

'd3Doctor'是id爲8的項目的名稱。在您的html中,您現在可以執行以下操作:'ID爲8的醫生:' – sroes

+0

so我可以獲得id 8的用戶名,地址,密碼.... – SpringLearner

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; 
      } 
     }); 
    }); 
}); 
+0

釷anks for answers,+ 1我會試試你的方式 – SpringLearner

+0

不客氣。 –

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

感謝回答+ 1,我從ajax獲取JSON,所以怎麼辦 – SpringLearner

+0

在獲取數據(在'jsondata')之後調用'findById'方法。 'findById'將會返回一個帶有相關醫生的'用戶名'和'email'的對象。 – mshsayem

+0

我在阿賈克斯的成功部分是說,我應該打電話?+1對您有所幫助 – SpringLearner

1

是。我可以回答這個問題。

在我的方式,我個人更喜歡把所有的數據到數組和itenerate,或操縱它。

在本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()

+0

你可以回答,使用knockout.js – SpringLearner

+0

分數已經提供了答案。 – jhyap

+0

是的,感謝您的回答,+ 1 – SpringLearner