2013-10-16 5 views
0

저는 JSON 디코딩에 비교적 익숙하지만,이 루핑을 위해이 형식을 사용하여 수행 할 수 있었으며 desk.com api. JSON 응답의 복잡성으로 인해 내 서식이 맘에 들지 않거나 키 -> 값 쌍을 얻는 더 좋은 방법이 있기 때문에 확실하지 않습니다. desk.com API의 JSON 응답을 반복합니다.

$topics = json_decode($response); 

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

이 주셔서 감사합니다 :

여기
{"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"}}}]}} 

내가 디코딩과 이름 값에 도착를 통해 반복 된 방법입니다 : 여기

이 API 호출에 대한 JSON 응답 ( http://dev.desk.com/API/topics/#list)입니다 도움. 여기

+0

를 작동하는지 알려주세요. 여기에 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. 

이 좀 쉬운 일이 아닙니다 json으로 문자열을 읽기

+0

고마워요 @om_deshpande - 일종의, 여전히 오류가 발생하지만, 그 후 제대로 이름을 출력합니다. 이렇게 생겼어. 통지서 : 오프셋 불확정 : 5 /Library/WebServer/Documents/afty/help/desk_api.php에서 라인 어레이 (125) (5) {[0] => 문자열 (18) "프라이버시 및 보안을"[1] => 문자열 (16) "준비된 답변"[2] => 캐릭터 (3) "FAQ"[3] => 문자열 (22) "추천 및 피드백"[4] => 끈 (7)에 "일반"} –

+0

@JeffSolomon 아. 작은 편집을했습니다. while 문 내부에 isset()이 추가되었습니다. 지금 일해야한다. –

+0

고마워요! 완전한. –

관련 문제