如何在 PHP 中回显 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9597941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:13:25  来源:igfitidea点击:

How to echo JSON in PHP

phpjsoncurlecho

提问by Itsyuka

I just need a little help with using cURL and JSON in PHP since I never actually got around to doing this. What I am trying to accomplish is to take the ign of the payment and echoing it, also limiting on how much can be posted by the latest donation first.

我只需要在 PHP 中使用 cURL 和 JSON 的一些帮助,因为我从来没有真正开始这样做。我想要完成的是采取付款的标志并回应它,同时限制最新的捐赠可以首先发布多少。

URL: http://api.buycraft.net/query?secret=83d043249170343dd048d4da62387f6cb39ed97f&action=payments

网址:http: //api.buycraft.net/query?secret=83d043249170343dd048d4da62387f6cb39ed97f&action=payments

回答by Bas Tuijnman

if you want to encode or decode an array from or to JSON you can use these functions

如果你想从 JSON 编码或解码数组,你可以使用这些函数

$myJSONString = json_encode($myArray);
$myArray = json_decode($myString);

json_encode will result in a JSON string, built from an (multi-dimensional) array. json_decode will result in an Array, built from a well formed JSON string

json_encode 将产生一个由(多维)数组构建的 JSON 字符串。json_decode 将产生一个数组,由一个格式良好的 JSON 字符串构建

with json_decode you can take the results from the API and only output what you want, for example:

使用 json_decode 您可以从 API 中获取结果并只输出您想要的内容,例如:

echo $myArray['payload']['ign'];

回答by jokkedk

Native JSON support has been includedin PHP since 5.2 in the form of methods json_encode()and json_decode(). You would use the first to output a PHP variable in JSON.

原生JSON支持已经列入,因为在方法的形式5.2 PHPjson_encode()json_decode()。您将使用第一个以 JSON 格式输出 PHP 变量。