2013-10-16 87 views
0

我對JSON解碼相對較新,但我已經能夠使用此格式(請參見下文)爲我的循環執行此操作,而且它似乎不適用於desk.com api。不知道是否因爲JSON響應的複雜性不像我的格式化,或者有更好的方法來獲取鍵 - >值對。從desk.com通過JSON響應循環API

下面是該API調用的JSON響應(http://dev.desk.com/API/topics/#list):

{"total_entries":4,"_links":{"self":{"href":"/api/v2/topics?page=1&per_page=50","class":"page"},"first":{"href":"/api/v2/topics?page=1&per_page=50","class":"page"},"last":{"href":"/api/v2/topics?page=1&per_page=50","class":"page"},"previous":null,"next":null},"_embedded":{"entries":[{"name":"Privacy & Security","description":"Information about your privacy.","position":1,"allow_questions":false,"in_support_center":true,"created_at":"2013-02-10T04:40:05Z","updated_at":"2013-09-26T00:12:13Z","_links":{"self":{"href":"/api/v2/topics/445877","class":"topic"},"articles":{"href":"/api/v2/topics/445877/articles","class":"article"},"translations":{"href":"/api/v2/topics/445877/translations","class":"topic_translation"}}},{"name":"Canned Responses","description":"Internal responses to common questions","position":3,"allow_questions":true,"in_support_center":false,"created_at":"2013-02-10T04:40:05Z","updated_at":"2013-09-26T00:31:25Z","_links":{"self":{"href":"/api/v2/topics/445878","class":"topic"},"articles":{"href":"/api/v2/topics/445878/articles","class":"article"},"translations":{"href":"/api/v2/topics/445878/translations","class":"topic_translation"}}},{"name":"FAQ","description":"Frequently Asked Questions","position":2,"allow_questions":false,"in_support_center":true,"created_at":"2013-02-10T04:40:05Z","updated_at":"2013-10-15T00:47:09Z","_links":{"self":{"href":"/api/v2/topics/445879","class":"topic"},"articles":{"href":"/api/v2/topics/445879/articles","class":"article"},"translations":{"href":"/api/v2/topics/445879/translations","class":"topic_translation"}}},{"name":"Suggestions & Feedback","description":"","position":4,"allow_questions":true,"in_support_center":true,"created_at":"2013-07-03T05:27:56Z","updated_at":"2013-10-16T02:38:11Z","_links":{"self":{"href":"/api/v2/topics/538220","class":"topic"},"articles":{"href":"/api/v2/topics/538220/articles","class":"article"},"translations":{"href":"/api/v2/topics/538220/translations","class":"topic_translation"}}}]}} 

這裏是我是如何解碼和循環通過去name值:

$topics = json_decode($response); 

foreach ($topics as $topic) { 
    echo "Name: " . $topic->_embedded->entries->name; 
} 

謝謝你幫助。

+0

讀JSON字符串是有點不容易。我們可以在這裏發佈var_dump($ topics)嗎? –

+0

好的,我沒有看到鏈接。忽略我以前的評論 –

回答

1

在這裏你去:

$entries = $topics->_embedded->entries; // 'entries' from the json response is an array. 
$i = 0; 
while(isset($entries[$i])) { // Loop through the array to pick up all the data you need 
$data[$i][0] = $entries[$i]->name; 
$data[$i][1] = $entries[$i]->description; 
$data[$i][2] = $entries[$i]->_links->self->href; 
$i++; 
} 
var_dump($data) // Array with all the data. Note that this is now a 2-d array. 

讓我知道這是否正常工作

+0

感謝@om_deshpande - 這種工作方式,仍然有錯誤,但它之後正確輸出名稱。看起來像這樣。 注意:未定義的偏移量:第5行/Library/WebServer/Documents/afty/help/desk_api.php on line 125 array(5){[0] => string(18)「Privacy&Security」[1] => string(16)「Canned Responses」[2] => string(3)「FAQ」[3] => string(22)「建議與反饋」[4] => string(7)「General」} –

+0

@JeffSolomon好吧。做了一個小小的編輯。在while語句中添加isset()。現在應該工作。 –

+0

謝謝!完善。 –