【问题标题】:Looping through JSON response from desk.com API循环来自desk.com API的JSON响应
【发布时间】:2013-10-16 03:32:05
【问题描述】:

我对 JSON 解码比较陌生,但我已经能够使用这种格式(见下文)进行循环,而且它似乎不适用于 desktop.com api。不确定是不是因为 JSON 响应的复杂性不喜欢我的格式,或者有更好的方法来获取键->值对。

这是此 API 调用 (http://dev.desk.com/API/topics/#list) 的 JSON 响应:

{"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;
}  

感谢您的帮助。

【问题讨论】:

  • 读取 json 字符串有点不容易。我们可以在这里发布 var_dump($topics) 吗?
  • 好的,我没有看到链接。忽略我之前的评论

标签: php json api


【解决方案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.

让我知道这是否有效

【讨论】:

  • 谢谢@om_deshpande -- 这有点工作,仍然会出错,但之后它会正确输出名称。看起来像这样。注意:未定义的偏移量:5 in /Library/WebServer/Documents/afty/help/desk_api.php on line 125 array(5) { [0]=> string(18) "Privacy & Security" [1]=> string( 16) “罐头回复”[2]=> 字符串(3)“常见问题解答”[3]=> 字符串(22)“建议和反馈”[4]=> 字符串(7)“常规”}
  • @JeffSolomon 啊,好吧。做了一个小修改。在 while 语句中添加了 isset()。现在应该可以工作了。
  • 对不起@om_deshpande,还有一个问题。在使用您的格式时,我可以获取“NAME”的单个元素,但是如果我想要条目中 JSON 响应中的其他元素,例如“描述”,例如下一个元素,我不知道如何用你的格式来做到这一点。感谢您继续提供帮助。
  • @JeffSolomon 如果您想要在 ENTRIES 中包含所有其他元素,您可以在 while 循环中运行 foreach 循环。如果你只想要一个或两个其他的,你可以在 while 循环内的一个单独的行中指定每个 - $name[$i][0] = $entries[$i]->name; $name[$i][1] = $entries[$i]->description;让我知道您在寻找以上哪一项;我会相应地更新我的答案中的代码。
  • 谢谢@om_deshpande,是的,只是从条目中的列表中寻找一两个其他元素,最重要的是名称、描述和主题 ID(我认为是从 _links->self-> href 如 API 文档中所示 - dev.desk.com/API/topics/#list
猜你喜欢
  • 2021-07-28
  • 2019-01-31
  • 1970-01-01
  • 1970-01-01
  • 2016-08-06
  • 2019-02-09
  • 1970-01-01
  • 1970-01-01
  • 2014-09-13
相关资源
最近更新 更多