我正在构建一个PHP脚本,将JSON数据提供给另一个脚本.我的脚本将数据构建为一个大的关联数组,然后使用输出数据json_encode.这是一个示例脚本:
$data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
header('Content-type: text/javascript');
echo json_encode($data);
Run Code Online (Sandbox Code Playgroud)
上面的代码产生以下输出:
{"a":"apple","b":"banana","c":"catnip"}
Run Code Online (Sandbox Code Playgroud)
如果您有少量数据,这很好,但我更喜欢这些内容:
{
"a": "apple",
"b": "banana",
"c": "catnip"
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有丑陋黑客的情况下在PHP中执行此操作?好像Facebook的某个人想出来了.
我只需要在PHP中使用cURL和JSON一点帮助,因为我从来没有真正开始这样做.我想要完成的是取消付款并回复它,同时限制最先捐赠的数量.
网址:http://api.buycraft.net/query?secret = 83d043249170343dd048d4da62387f6cb39ed97f&action = payments
这是JSON,它以异步方式发送到我的php页面.它本质上是一个产品列表,将被插入到我的mySQL数据库中.
我的问题是在PHP中解码JSON.我可以使用'eval'函数在js中做到这一点,但在PHP中,我的努力导致了一系列复杂的爆炸和内爆函数.
{
"Product": [
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
},
{
"Product_Title": "Cloth",
"Product_Description": "Here is cloth",
"Price": "100",
"Category_ID": "1"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我知道php有一个内置的json_decode函数,但在PHP文档中它们只显示了如何处理数组.
任何建议或帮助都非常感谢
泰勒