将JSON转换为PHP数组 [英] Convert JSON into PHP Array

查看:88
本文介绍了将JSON转换为PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在工作中使用Zendesk,而我使用的是PHP&他们提供的CURL显示数据.但是,如果我使用(json_decode($ output,true)),我得到的数据就会像这样返回:

We use Zendesk at work and I am using some PHP & CURL provided by them to display data. However the Data I get comes back like this if I use (json_decode($output,true)) :

array(4){["satisfaction_ratings"] => array(2){[0] => array(10){["url"] =>字符串(83)" int(183839137)[" assignee_id] => int(551715796)[" group_id] => int(21464896)[" requester_id] => int(543065527)[" ticket_id] => int( 6)["score"] => string(4)"good" ["created_at"] => string(20)"2013-11-22T12:57:50Z" ["updated_at"] => string(20)"2013-11-22T12:57:50Z"["注释] =>字符串(3)"嘿} [1] =>数组(10){[""url"] =>字符串(83)" https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json "[" id] => int(180738208)[" assignee_id] => int(551715796)[" group_id] => int(21464896)[" requester_id] => int(543078357)[" ticket_id"] => int(7)[分数"] => string(4)好"[" created_at] =>字符串(20)" 2013-11-22T13:16:11Z"[" updated_at] =>字符串(20)" 2013-11-22T13:16:11Z"["评论"] =>字符串(5)"heyyy"}} [下一页"] => NULL [上一页"] => NULL ["count"] => int(2)}

array(4) { ["satisfaction_ratings"]=> array(2) { [0]=> array(10) { ["url"]=> string(83) "https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json" ["id"]=> int(183839137) ["assignee_id"]=> int(551715796) ["group_id"]=> int(21464896) ["requester_id"]=> int(543065527) ["ticket_id"]=> int(6) ["score"]=> string(4) "good" ["created_at"]=> string(20) "2013-11-22T12:57:50Z" ["updated_at"]=> string(20) "2013-11-22T12:57:50Z" ["comment"]=> string(3) "hey" } [1]=> array(10) { ["url"]=> string(83) "https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json" ["id"]=> int(180738208) ["assignee_id"]=> int(551715796) ["group_id"]=> int(21464896) ["requester_id"]=> int(543078357) ["ticket_id"]=> int(7) ["score"]=> string(4) "good" ["created_at"]=> string(20) "2013-11-22T13:16:11Z" ["updated_at"]=> string(20) "2013-11-22T13:16:11Z" ["comment"]=> string(5) "heyyy" } } ["next_page"]=> NULL ["previous_page"]=> NULL ["count"]=> int(2) }

如果我仅使用json_decode($ output),则会得到:

If I use just json_decode($output) I get:

object(stdClass)#1(4){["satisfaction_ratings"] => array(2){[0] => object(stdClass)#2(10){["url"] =>字符串(83) " https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137. json " ["id"] =>整数(183839137)["assignee_id"] =>整数(551715796) ["group_id"] =>整数(21464896)["requester_id"] =>整数(543065527) ["ticket_id"] => int(6)["score"] => string(4)"good" ["created_at"] => 字符串(20)"2013-11-22T12:57:50Z" ["updated_at"] =>字符串(20) "2013-11-22T12:57:50Z" [评论"] =>字符串(3)嘿"} [1] => object(stdClass)#3(10){["url"] =>字符串(83) " https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208. json " ["id"] =>整数(180738208)["assignee_id"] =>整数(551715796) ["group_id"] =>整数(21464896)["requester_id"] =>整数(543078357) ["ticket_id"] => int(7)["score"] => string(4)"good" ["created_at"] => 字符串(20)"2013-11-22T13:16:11Z" ["updated_at"] =>字符串(20) "2013-11-22T13:16:11Z" [评论"] => string(5)"heyyy"}} ["next_page"] => NULL ["previous_page"] => NULL ["count"] => int(2)}

object(stdClass)#1 (4) { ["satisfaction_ratings"]=> array(2) { [0]=> object(stdClass)#2 (10) { ["url"]=> string(83) "https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json" ["id"]=> int(183839137) ["assignee_id"]=> int(551715796) ["group_id"]=> int(21464896) ["requester_id"]=> int(543065527) ["ticket_id"]=> int(6) ["score"]=> string(4) "good" ["created_at"]=> string(20) "2013-11-22T12:57:50Z" ["updated_at"]=> string(20) "2013-11-22T12:57:50Z" ["comment"]=> string(3) "hey" } [1]=> object(stdClass)#3 (10) { ["url"]=> string(83) "https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json" ["id"]=> int(180738208) ["assignee_id"]=> int(551715796) ["group_id"]=> int(21464896) ["requester_id"]=> int(543078357) ["ticket_id"]=> int(7) ["score"]=> string(4) "good" ["created_at"]=> string(20) "2013-11-22T13:16:11Z" ["updated_at"]=> string(20) "2013-11-22T13:16:11Z" ["comment"]=> string(5) "heyyy" } } ["next_page"]=> NULL ["previous_page"]=> NULL ["count"]=> int(2) }

基本上,我想做的只是获取评论"部分的列表.我试图将此行用于json_decode($ output):print $ data-> satisfaction_ratings-> comment;

Essentially what I'm trying to do is just get a list of the "comment" sections. I tried to use this line for the json_decode($output): print $data->satisfaction_ratings->comment;

但是那行不通,任何人都可以协助获取一个数组值吗?我无法更改系统中的JSON.通过jason_decode运行之前的JSON是:

But that doesnt work, can anyone assist with getting just the one array value out?, I am not able to change the JSON that comes out of the system. The JSON before it runs through jason_decode is:

字符串(666) "{" satisfaction_ratings:[{" url:" https://sessioncam1384769452. zendesk.com/api/v2/satisfaction_ratings/183839137.json ," id:183839137," assignee_id:551715796," group_id:21464896," requester_id:543065527," ticket_id:6,"评分:"好," created_at:" 2013-11-22T12:57:50Z," updated_at:" 2013-11-22T12:57:50Z,"评论:"嘿},{" url:" https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings /180738208.json ," id:180738208," assignee_id:551715796," group_id:21464896," requester_id:543078357," ticket_id:7," score:" good," created_at :" 2013-11-22T13:16:11Z," updated_at:" 2013-11-22T13:16:11Z,"评论:" heyyy}]," next_page:null," previous_page: null,"count":2}"

string(666) "{"satisfaction_ratings":[{"url":"https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json","id":183839137,"assignee_id":551715796,"group_id":21464896,"requester_id":543065527,"ticket_id":6,"score":"good","created_at":"2013-11-22T12:57:50Z","updated_at":"2013-11-22T12:57:50Z","comment":"hey"},{"url":"https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json","id":180738208,"assignee_id":551715796,"group_id":21464896,"requester_id":543078357,"ticket_id":7,"score":"good","created_at":"2013-11-22T13:16:11Z","updated_at":"2013-11-22T13:16:11Z","comment":"heyyy"}],"next_page":null,"previous_page":null,"count":2}"

如果有人能帮上忙,那就太好了

If anyone can help that would be great

推荐答案

如果要访问特定于元素,则可以使用以下内容:

If you want to access that specific element, so you can use the following:

echo $data->satisfaction_ratings[0]->comment;
echo $data->satisfaction_ratings[1]->comment;

如果要将json_decode()的第二个参数设置为TRUE,则需要使用数组语法来访问注释:

If you're setting the second parameter for json_decode() as TRUE, then you need to use the array syntax to access the comment:

echo $data['satisfaction_ratings'][0]['comment'];
echo $data['satisfaction_ratings'][1]['comment'];

如果有多个注释,并且要显示所有注释,请使用循环:

If there are multiple comments, and you want to display all of them, use a loop:

foreach ($data['satisfaction_ratings'] as $comment) {
    echo $comment['comment'] . '<br/>';
}

这篇关于将JSON转换为PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆