2013-04-14 47 views
2

所以我在JavaScript中,需要一個用戶的照片,然後發送它關閉面部識別API的應用程序,它返回一個JSON值,然後我有轉換爲字符串。但我只有兩個返回到我的項目是important.I要拔出這兩個(UID和信心),並將它們設置爲變量;我不完全知道該怎麼做。下面是什麼是返回一個例子:如何拔出從返回JSON字符串的某些數據?

{"photos":[{"url":"http://api.skybiometry.com/fc/images/get?id=bmN2X3hybD0wMTk5c29ub3I0MXE0bjN…RyMTQzJmVxPTE1ODkmY3ZxPTY5MjcxMzEyMTI3cTImZ3Z6cmZnbnpjPTIwMTMwNDE0MTgxNjI5","pid":"[email protected]_69271312127d2","width":380,"height":300,"tags":[{"tid":"[email protected]_69271312127d2_51.05_45.67_0_1","recognizable":true,"threshold":20,"uids":[{"uid":"[email protected]","confidence":100}],"confirmed":false,"manual":false,"width":20,"height":25.33,"center":{"x":51.05,"y":45.67},"eye_left":{"x":56.32,"y":39.67},"eye_right":{"x":46.32,"y":40},"mouth_center":{"x":48.68,"y":55.67},"nose":{"x":50.79,"y":47},"yaw":0,"roll":1,"pitch":0,"attributes":{"face":{"value":"true","confidence":60}}}]}],"status":"success","usage":{"used":5,"remaining":95,"limit":100,"reset_time_text":"Sun, 14 April 2013 18:20:53 +0000","reset_time":1365963653}} 

下面是代碼我目前使用的是現在返回整個字符串:

.done(function (result) { 
    alert("Received response.."); 
    var resultObject = JSON.stringify(result); 
    alert(resultObject); 
}); 

任何幫助,不勝感激!

+0

http://api.jquery.com/jQuery.getJSON/已讀 – smerny

+0

JSON.parse()來未JSON.stringify() – monkeyinsight

+0

resultObject [ 'UID']和resultObject [ '信心']? – NINCOMPOOP

回答

4
.done(function (result) { 
    if(result != null) { 
     var uids = result.photos[0].tags[0].uids[0]; 
     var uid = uids.uid; 
     var confidence = uids.confidence; 

    } }); 
+1

第三行應該是'變種的uid = result.photos [0] .tags添加[0] .uids [0];'。 – brezanac

+0

所以jsfiddle說我應該使用!==比較null,所以繼承人編輯的代碼,我有:.done(函數(結果){ 如果(結果!== null){var 0ids = result.photos [0 ] .tags添加[0] .uids [0]; 變種UID = uids.uid; 變種信心= uids.confidence; 警報(UID); }}); – user2266621

+0

,並在控制檯中給出錯誤:Uncaught TypeError:無法讀取未定義的屬性'uids' – user2266621

相關問題