如何实现PHP解析不规范非标准JSON数据的示例代码?

更新于
2026-09-28 16:26:14
1阅读来源:SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计456个文字,预计阅读时间需要2分钟。

如何实现PHP解析不规范非标准JSON数据的示例代码?

前言:采集数据的时经常遇到一些JSON数据的Key值不是字符串的情况,下面来看看用PHP如何解析非标准、非规范的JSON。

下面直接上代码:

php

// 使用json_decode解析JSON字符串,第二个参数设置为true,表示将解析后的结果转换为关联数组$data=json_decode($json, true);

// 输出解析后的数据print_r($data);?>

输出结果:

Array( [str]=> Array ( [0]=> aaaa [1]=> bb ))

注意:这里假设JSON数据格式正确,实际使用时可能需要根据实际情况进行调整。

如何实现PHP解析不规范非标准JSON数据的示例代码?

前言

采集数据的时候经常碰到一些JSON数据的Key值不是字符串,下面就来看看用php如何解析非标准、非规范json

话不多说,直接上代码:

// 解析非标准json,注意暂时未处理 {str:['aaaa','bbb']} $json = preg_replace( "/\s(?=\s)/","\\1", $json );// 多个空格只留一个 $json = preg_replace( "/([\{\}\,]+)\s?'?\s?(\w*?)\s?'?\s?:\s?/", '\\1"\\2":', $json);// 转义key $json = preg_replace("/\"\s?:\'\s?(.*?)\'\s?([\,\]\}]+?)/",'":"\\1"\\2', $json);// 转义 value // 解析非标准json,注意暂时未处理 {str:['aaaa','bbb']}

// 测试数据 $json[] = "{'v-soft-list':[{typecode:'zhuanbi',gametype:'赚币',count:5},{typecode:'dongzuo',gametype:'动作',count:12},{typecode:'maoxian',gametype:'冒险',count:14},{typecode:'xiuxian',gametype:'休闲',count:61},{typecode:'zhili',gametype:'智力',count:17},{typecode:'gaoxiao',gametype:'搞笑',count:6},{typecode:'celue',gametype:'策略',count:4},{typecode:'qipai',gametype:'棋牌',count:2}]}"; $json[] ="{'asin': '0001048791', 'salesRank': {'Books': 6334800}, 'imUrl': 'img.558idc.com/uploadfile/allimg/php/51MKP0T4DBL.jpg', 'categories': [['Books']], 'title': 'The Crucible: Performed by Stuart Pankin, Jerome Dempsey & Cast'}"; $json[] = "{csrfToken: 'ffmekos3s3kc', abVersion: '', abtestMap: {}}";

附php解析JSON数据示例代码:

data.json

{ "name":”yudabo“, "score":100 }

PHP

$json_string = file_get_contents('json/data.json'); $data = json_decode($json_string, true); echo $data['name'].' '.$data['score'];

到此这篇关于php解析非标准json、非规范json的文章就介绍到这了,更多相关php解析非标准、非规范json内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

标签:方式实例

本文共计456个文字,预计阅读时间需要2分钟。

如何实现PHP解析不规范非标准JSON数据的示例代码?

前言:采集数据的时经常遇到一些JSON数据的Key值不是字符串的情况,下面来看看用PHP如何解析非标准、非规范的JSON。

下面直接上代码:

php

// 使用json_decode解析JSON字符串,第二个参数设置为true,表示将解析后的结果转换为关联数组$data=json_decode($json, true);

// 输出解析后的数据print_r($data);?>

输出结果:

Array( [str]=> Array ( [0]=> aaaa [1]=> bb ))

注意:这里假设JSON数据格式正确,实际使用时可能需要根据实际情况进行调整。

如何实现PHP解析不规范非标准JSON数据的示例代码?

前言

采集数据的时候经常碰到一些JSON数据的Key值不是字符串,下面就来看看用php如何解析非标准、非规范json

话不多说,直接上代码:

// 解析非标准json,注意暂时未处理 {str:['aaaa','bbb']} $json = preg_replace( "/\s(?=\s)/","\\1", $json );// 多个空格只留一个 $json = preg_replace( "/([\{\}\,]+)\s?'?\s?(\w*?)\s?'?\s?:\s?/", '\\1"\\2":', $json);// 转义key $json = preg_replace("/\"\s?:\'\s?(.*?)\'\s?([\,\]\}]+?)/",'":"\\1"\\2', $json);// 转义 value // 解析非标准json,注意暂时未处理 {str:['aaaa','bbb']}

// 测试数据 $json[] = "{'v-soft-list':[{typecode:'zhuanbi',gametype:'赚币',count:5},{typecode:'dongzuo',gametype:'动作',count:12},{typecode:'maoxian',gametype:'冒险',count:14},{typecode:'xiuxian',gametype:'休闲',count:61},{typecode:'zhili',gametype:'智力',count:17},{typecode:'gaoxiao',gametype:'搞笑',count:6},{typecode:'celue',gametype:'策略',count:4},{typecode:'qipai',gametype:'棋牌',count:2}]}"; $json[] ="{'asin': '0001048791', 'salesRank': {'Books': 6334800}, 'imUrl': 'img.558idc.com/uploadfile/allimg/php/51MKP0T4DBL.jpg', 'categories': [['Books']], 'title': 'The Crucible: Performed by Stuart Pankin, Jerome Dempsey & Cast'}"; $json[] = "{csrfToken: 'ffmekos3s3kc', abVersion: '', abtestMap: {}}";

附php解析JSON数据示例代码:

data.json

{ "name":”yudabo“, "score":100 }

PHP

$json_string = file_get_contents('json/data.json'); $data = json_decode($json_string, true); echo $data['name'].' '.$data['score'];

到此这篇关于php解析非标准json、非规范json的文章就介绍到这了,更多相关php解析非标准、非规范json内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

标签:方式实例