JSON解码显示“Array”

编程入门 行业动态 更新时间:2024-10-24 20:18:52
本文介绍了JSON解码显示“Array”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这部分:

$ response_2 = json_decode($ response_2,true);

...下面的代码在浏览器中被称为Array。如果我删除部分,完整的$ response_2在浏览器中以JSON格式回显,就像这个例子: developer.spotify/web-api/get-list-users-playlists/

如何

<?php $ url ='https://accounts.spotify。 com / api / token'; $ method ='POST'; $ credentials =hidden:hidden; $ headers = array(Accept:* / *,Content-Type:application / x-www-form-urlencoded,授权:基本。base64_encode($ credentials)); $ data ='grant_type = client_credentials'; $ ch = curl_init(); curl_setopt($ ch,CURLOPT_URL,$ url); curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers); curl_setopt($ ch,CURLOPT_POST,1); curl_setopt($ ch,CURLOPT_POSTFIELDS,$ data); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); $ response = curl_exec($ ch); curl_close($ ch); $ response = json_decode($ response,true); $ token = $ response ['access_token']; echo我的令牌是:。 $ token; $ headers_2 = array(Accept:* / *,Content-Type:application / x-www-form-urlencoded 'Authorization:Bearer'。$ token)); $ ch = curl_init(); curl_setopt($ ch,CURLOPT_URL,'https://api.spotify/v1/users/wizzler/playlists'); curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers_2); curl_setopt($ ch,CURLOPT_POST,false); curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true); $ response_2 = curl_exec($ ch); curl_close($ ch); $ response_2 = json_decode($ response_2,true); echo $ response_2; ?>当你使用 echo

在数组上,它只是打印出字符串 Array 。这只是PHP的一个奇怪。

如果要打印数组的内容,可以使用 print_r()或 var_dump()。

但是,你真正想要做的是打印JSON,这是字符串。 $ response_2 已是一个字符串,因此请将其打印出来。

This part:

$response_2 = json_decode($response_2, true);

...of the code below is echoed literally as "Array" in the browser. If I remove the part, the full $response_2 is echoed in browser in JSON-format just like in this example: developer.spotify/web-api/get-list-users-playlists/

How come?

<?php $url = 'accounts.spotify/api/token'; $method = 'POST'; $credentials = "hidden:hidden"; $headers = array( "Accept: */*", "Content-Type: application/x-www-form-urlencoded", "Authorization: Basic " . base64_encode($credentials)); $data = 'grant_type=client_credentials'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); $response = json_decode($response, true); $token = $response['access_token']; echo "My token is: " . $token; $headers_2 = array( "Accept: */*", "Content-Type: application/x-www-form-urlencoded", ('Authorization: Bearer ' . $token)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,'api.spotify/v1/users/wizzler/playlists'); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_2); curl_setopt($ch, CURLOPT_POST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response_2 = curl_exec($ch); curl_close($ch); $response_2 = json_decode($response_2, true); echo $response_2; ?>

解决方案

When you use echo on an array, it just prints out the literal string Array. This is just a quirk of PHP.

If you want to print out the contents of the array, you can use print_r() or var_dump().

However it seems like what you actually want to do is print the JSON, which is the string. $response_2 is already a string, so print it out.

更多推荐

JSON解码显示“Array”

本文发布于:2023-08-03 08:07:38,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   Array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!