如何从返回的JSON字符串中提取某些数据?

时间:2013-04-14 18:33:46

标签: javascript json string

所以我在JavaScript中有一个应用程序,用来拍摄用户照片,然后将其发送到面部识别API并返回一个JSON值,然后我将其转换为字符串。但是我只返回给我的两个项目很重要。我想把这两个项目(uid和置信度)拉出来并将它们设置为变量;我不完全确定怎么做。以下是返回内容的示例:

{"photos":[{"url":"http://api.skybiometry.com/fc/images/get?id=bmN2X3hybD0wMTk5c29ub3I0MXE0bjN…RyMTQzJmVxPTE1ODkmY3ZxPTY5MjcxMzEyMTI3cTImZ3Z6cmZnbnpjPTIwMTMwNDE0MTgxNjI5","pid":"F@0deeecd9274c7d8357343ed57e6aaf6c_69271312127d2","width":380,"height":300,"tags":[{"tid":"TEMP_F@0deeecd9274c7d8357343ed500c20089_69271312127d2_51.05_45.67_0_1","recognizable":true,"threshold":20,"uids":[{"uid":"sam@sam","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);
});

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:4)

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

     } });