- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有无效的外部 json 数据,名称周围没有双引号。
示例:
{
data: [
{
idx: 0,
id: "0",
url: "http://247wallst.com/",
a: [
{
t: "Title",
u: "http://247wallst.com/2012/07/30/",
sp: "About"
}
],
doc_id: "9386093612452939480"
},
{
idx: 1,
id: "-1"
}
],
results_per_page: 10,
total_number_of_news: 76,
news_per_month: [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
result_start_num: 2,
result_end_num: 2,
result_total_articles: 76
}
正如你看到的很多名称,如 data、idx、id、url 等都没有用双引号括起来,所以这使得这个 json 无效。如何使这个外部 json 有效?我已经尝试过 str_replace,将 '{' 替换为 '{"',将 ':' 替换为 '":',在未加引号的名称周围添加双引号,但这会弄乱一些已经用双引号引起来的变量。
如何使此 json 有效,以便我可以使用 PHP json_decode 读取此数据?我对 preg_replace 不太熟悉..
有效的 json 看起来像:
{
"data": [
{
"idx": 0,
"id": "0",
"url": "http://247wallst.com/",
"a": [
{
"t": "Title",
"u": "http://247wallst.com/2012/07/30/",
"sp": "About"
}
],
"doc_id": "9386093612452939480"
},
{
"idx": 1,
"id": "-1"
}
],
"results_per_page": 10,
"total_number_of_news": 76,
"news_per_month": [20, 0, 8, 1, 1, 2, 0, 2, 1, 0, 0, 1, 1, 0, 5, 1, 1, 1, 0, 2, 5, 16, 7, 1],
"result_start_num": 2,
"result_end_num": 2,
"result_total_articles": 76
}
请给我推荐一些 php preg_replace 函数。
数据来源: http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1
最佳答案
使用 preg_replace
您可以执行以下操作:
json_decode(preg_replace('#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', $in));
由于上面的例子不适用于真实数据(作战计划很少能在与敌人的第一次接触中幸存下来),这是我的第二个想法:
$infile = 'http://www.google.com/finance/company_news?q=aapl&output=json&start=1&num=1';
// first, get rid of the \x26 and other encoded bytes.
$in = preg_replace_callback('/\\\x([0-9A-F]{2})/i',
function($match){
return chr(intval($match[1], 16));
}, file_get_contents($infile));
$out = $in;
// find key candidates
preg_match_all('#(?<=\{|\[|,)\s*(?<key>(?:\w|_)+?)\s*:#im', $in, $m, PREG_OFFSET_CAPTURE);
$replaces_so_far = 0;
// check each candidate if its in a quoted string or not
foreach ($m['key'] as $match) {
$position = $match[1] + ($replaces_so_far * 2); // every time you expand one key, offsets need to be shifted with 2 (for the two " chars)
$key = $match[0];
$quotes_before = preg_match_all('/(?<!\\\)"/', substr($out, 0, $position), $m2);
if ($quotes_before % 2) { // not even number of not-escaped quotes, we are in quotes, ignore candidate
continue;
}
$out = substr_replace($out, '"'.$key.'"', $position, strlen($key));
++$replaces_so_far;
}
var_export(json_decode($out, true));
但是由于 google 在 RSS feed 中提供了这些数据,如果它适合您的用例,我建议您使用该数据,这只是为了好玩(-:
关于PHP 使用 json_decode() 读取无效的 json;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11729051/
json_decode($var) 和 (object)json_decode($var, true) 有区别吗? 当最近在 Joomla virutemart 中处理某段代码时,我遇到了一个困惑的情
我正在尝试使用 PHP 和 json_decode 解析 JSON 文件,但是当返回的 JSON 是命名空间时,我很难做到这一点。例如: $json_ouput = json_decode($json
我返回了这个 RAW JSON,当我应用 json_decode 以数组形式访问数据时,它会将 trackingNumber 转换为科学记数法。有没有什么可能的方法可以避免这种情况而不会影响/舍入整数
当我将带有前导零数字的电话号码(即字符串)传递给 json_decode 时, 它删除第一个零字符并将其转换为浮点数。 $string = '09178882356'; //dynamic value
由于某种原因,我无法从返回的 jsonp 字符串中获取信息, add ?vorto=TheWordYouWant to the end of this website"; echo $Help;
我只是从 php 文档测试这个示例 ( http://au2.php.net/manual/en/function.json-decode.php ) 这是我的代码: ';?> 但它只返回一个空数组。
我有一个包含多个对象的 JSON 数组,并尝试使用 json_decode 来创建关联数组。 示例数据 $json='[{ type: "cool", cate
我有类似以下代码: 结果如下: int(9223372036854775807) 当然,这不是 json 字符串,我希望 json_decode 返回 null 或 false,如 manual 中
我有一个以下 JSON 字符串,通过 AJAX 到达服务器: {"Names":"[{0:'asdasd'}]","Values":"[{0:'ad'}]"} 如您所见,名称和值旨在保存一个数组。问题
通过示例最容易解释: var_dump(json_decode("[{'a':'b'},{'c':'d'},{'e':'f'}]")); // NULL var_dump(json_decode('[
我正在使用来自 deepl.com 的 API,它提供自动翻译。我通过 cURL 调用 API,并得到一个 json 字符串作为返回,该字符串看起来没问题,但由于某种原因无法被 PHP 解码。 让我首
我有这样编码的 JSON 对象 { "id": "", "steps": [ { "target": "purchase_order_items_itemma
这个问题在这里已经有了答案: Get information from JSON (2 个答案) 关闭 11 个月前。 我一直在尝试使用来自各种服务的一些 API,目前我正在尝试检索有关 vimeo
我有一个包含多个对象的 JSON 数组,并尝试使用 json_decode 来创建一个关联数组。 示例数据 $json='[{ type: "cool", ca
我有类似以下代码: 结果如下: int(9223372036854775807) 当然,这不是 json 字符串,我希望 json_decode 返回 null 或 false,如 manual 中
通过示例最容易解释: var_dump(json_decode("[{'a':'b'},{'c':'d'},{'e':'f'}]")); // NULL var_dump(json_decode('[
我有一个像这样的二维对象数组: function test(n){ this.id = n; } var testArray= new Array(2); for(i = 0; i 但是我得到一
我正在以这种方式在 javascript 中创建一个 json jsonArr.push({ position: 'WN', wind: windWN,
$objetos = json_decode($_POST['objetos']); $query1 = "DELETE FROM `usuarioObjeto` WHERE idusuario="
我刚刚开始真正使用 json,并尝试尽我所能地学习!我想分享我所做的这项工作,我觉得这可能需要一些改进,如果不是大部分的话。 好吧,我使用 twitch.tv REST_API。这是我的代码。基本上我
我是一名优秀的程序员,十分优秀!