将JSON解码为PHP [英] Decode JSON to PHP
问题描述
我有一个名为'test.txt'的.txt文件,它是一个像这样的JSON数组:
I have a .txt file called 'test.txt' that is a JSON array like this:
[{"email":"chrono@gmail.com","createdate":"2016-03-23","source":"email"}]
我正在尝试使用PHP解码此JSON数组,以便可以将信息发送到我的电子邮件数据库以进行捕获.我用以下代码创建了一个PHP文件:
I'm trying to use PHP to decode this JSON array so I can send my information over to my e-mail database for capture. I've created a PHP file with this code:
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
echo $json;
?>
由于某种原因,当我访问我的php页面时,它没有回显解码的JSON.有这个原因吗?有人可以阐明一下吗?谢谢!
For some reason, it's not echoing the decoded JSON when I visit my php page. Is there a reason for this and can anyone shed some light? Thanks!
推荐答案
您需要将json字符串拆分为两个单独的json字符串(根据您提供的pastebin判断).寻找] [",在此处中断,然后尝试使用最后得到的任何部分:
You'll need to split that json string into two separate json strings (judging by the pastebin you've provided). Look for "][", break there, and try with any of the parts you end up with:
$tmp = explode('][', $json_string);
if (!count($tmp)) {
$json = json_decode($json_string);
var_dump($json);
} else {
foreach ($tmp as $json_part) {
$json = json_decode('['.rtrim(ltrim($json_string, '['), ']').']');
var_dump($json);
}
}
这篇关于将JSON解码为PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!