2016-09-14 100 views
0

我想從我的網站調用外部API系統。在ajax響應中,我得到以下數據。如何從json響應中獲取特定字段值

object(stdClass)#3 (3) { 


["status"]=> 
    string(7) "success" 
    ["code"]=> 
    int(200) 
    ["data"]=> 
    object(stdClass)#4 (3) { 

    ["desktop_url"]=> 
    string(113) "http://landing.beta.learning.social/remote?token=a_token_value" 

    ["mobile_url"]=> 
    string(115) "http://m.landing.beta.learning.social/remote?token=a_token_value" 

    } 
} 

我需要從這個response.So,我可以從field.I曾嘗試用下面的代碼的URL重定向頁面獲得desktop_url字段的值。

$.ajax({ 
    type: "POST", 
    url: path, 
    data: "email=" + $("#email_data").val() + "&fname=" + $("#fname_data").val()+"&grade="+$("#gradeID_data").val()+ 
    "&lastname="+$("#lname_data").val(), 
    success: function(message){ 
    $(message).map(function(item){console.log (item.data);}); 
    } 
}); 

這不是爲我工作,給我下面的錯誤

Error: Syntax error, unrecognized expression: object(stdClass)#3 (3... 
+0

的響應,這不是一個有效的JSON數組。 –

回答

3

您需要的JSON編碼由API

<?php 
$response = $api->call($params[,...]); 


header('content-type:application/json'); 
exit(json_encode($response)); 
+0

當我嘗試在api調用中對響應進行編碼時,我得到如下響應:'string(416)「」{\「status \」:\「success \」,\「code \」:200,\「 data \「:{\」token \「:\」token \「,\」desktop_url \「:\」http:\\\ \ \ \\/landing.beta.learning.social \\\/remote?token = token \「,\」mobile_url \「:\」http:\\\ \ \ \ \ \/m.landing.beta.learning.social \\\/remote?token = token \「}}」「' – Techy

+0

嘗試解碼響應'json_decode($ response,true);'或'$ responseStr = var_export($ response,true);' –

+0

看起來不錯,你現在可以從'console.log'中獲得'desktop_url' .data.desktop_url)'在你的成功功能中 – andrew