0

私は JSON デコードに比較的慣れていませんが、ループにこの形式 (以下を参照) を使用して実行できましたが、desk.com API では機能しないようです。JSON 応答の複雑さが私の書式設定に合わないためなのか、それともキーと値のペアを取得するためのより良い方法があるためなのかはわかりません。

この 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;
}  

お手伝いありがとう。

4

1 に答える 1