json格式转数组注意事项
今天遇到一个特别奇葩的问题,json格式明明是正确的,转数组时却就是出不来,后来才发现是文件的编码问题,文件的编码一定要是utf-8无BOM格式,这点一定要切记!切记!
如果想确认json是否正确:http://www.kjson.com/
json格式的城市:
<?php
$json = '{
"北京": {
"key": 11,
"defaultvalue": 1,
"values": {
"东城区": 1,
"西城区": 2,
"崇文区": 3,
"宣武区": 4,
"朝阳区": 5,
"丰台区": 6,
"石景山区": 7,
"海淀区": 8,
"门头沟区": 9,
"房山区": 11,
"通州区": 12,
"顺义区": 13,
"昌平区": 14,
"大兴区": 15,
"怀柔区": 16,
"平谷区": 17,
"密云县": 28,
"延庆县": 29 //注意:此处不能有逗号,否则会出错
}
},
"天津": {
"key": 12,
"defaultvalue": 1,
"values": {
"和平区": 1,
"河东区": 2,
"河西区": 3,
"南开区": 4,
"河北区": 5,
"红桥区": 6,
"塘沽区": 7,
"汉沽区": 8,
"大港区": 9,
"东丽区": 10,
"西青区": 11,
"津南区": 12,
"北辰区": 13,
"武清区": 14,
"宝坻区": 15,
"宁河县": 21,
"静海县": 23,
"蓟县": 25
}
},
"河北": {
"key": 13,
"defaultvalue": 1,
"values": {
"石家庄": 1,
"唐山": 2,
"秦皇岛": 3,
"邯郸": 4,
"邢台": 5,
"保定": 6,
"张家口": 7,
"承德": 8,
"沧州": 9,
"廊坊": 10,
"衡水": 11
}
},
"山西": {
"key": 14,
"defaultvalue": 1,
"values": {
"太原": 1,
"大同": 2,
"阳泉": 3,
"长治": 4,
"晋城": 5,
"朔州": 6,
"晋中": 7,
"运城": 8,
"忻州": 9,
"临汾": 10,
"吕梁": 23
}
},
"内蒙古": {
"key": 15,
"defaultvalue": 1,
"values": {
"呼和浩特": 1,
"包头": 2,
"乌海": 3,
"赤峰": 4,
"通辽": 5,
"鄂尔多斯": 6,
"呼伦贝尔": 7,
"兴安盟": 22,
"锡林郭勒盟": 25,
"乌兰察布盟": 26,
"巴彦淖尔盟": 28,
"阿拉善盟": 29
}
},
"辽宁": {
"key": 21,
"defaultvalue": 1,
"values": {
"沈阳": 1,
"大连": 2,
"鞍山": 3,
"抚顺": 4,
"本溪": 5,
"丹东": 6,
"锦州": 7,
"营口": 8,
"阜新": 9,
"辽阳": 10,
"盘锦": 11,
"铁岭": 12,
"朝阳": 13,
"葫芦岛": 14
}
},
"吉林": {
"key": 22,
"defaultvalue": 1,
"values": {
"长春": 1,
"吉林": 2,
"四平": 3,
"辽源": 4,
"通化": 5,
"白山": 6,
"松原": 7,
"白城": 8,
"延边朝鲜族自治州": 24
}
},
"黑龙江": {
"key": 23,
"defaultvalue": 1,
"values": {
"哈尔滨": 1,
"齐齐哈尔": 2,
"鸡西": 3,
"鹤岗": 4,
"双鸭山": 5,
"大庆": 6,
"伊春": 7,
"佳木斯": 8,
"七台河": 9,
"牡丹江": 10,
"黑河": 11,
"绥化": 12,
"大兴安岭": 27
}
},
"上海": {
"key": 31,
"defaultvalue": 1,
"values": {
"黄浦区": 1,
"卢湾区": 3,
"徐汇区": 4,
"长宁区": 5,
"静安区": 6,
"普陀区": 7,
"闸北区": 8,
"虹口区": 9,
"杨浦区": 10,
"闵行区": 12,
"宝山区": 13,
"嘉定区": 14,
"浦东新区": 15,
"金山区": 16,
"松江区": 17,
"青浦区": 18,
"南汇区": 19,
"奉贤区": 20,
"崇明县": 30
}
},
"江苏": {
"key": 32,
"defaultvalue": 1,
"values": {
"南京": 1,
"无锡": 2,
"徐州": 3,
"常州": 4,
"苏州": 5,
"南通": 6,
"连云港": 7,
"淮安": 8,
"盐城": 9,
"扬州": 10,
"镇江": 11,
"泰州": 12,
"宿迁": 13
}
},
"浙江": {
"key": 33,
"defaultvalue": 1,
"values": {
"杭州": 1,
"宁波": 2,
"温州": 3,
"嘉兴": 4,
"湖州": 5,
"绍兴": 6,
"金华": 7,
"衢州": 8,
"舟山": 9,
"台州": 10,
"丽水": 11
}
},
"安徽": {
"key": 34,
"defaultvalue": 1,
"values": {
"合肥": 1,
"芜湖": 2,
"蚌埠": 3,
"淮南": 4,
"马鞍山": 5,
"淮北": 6,
"铜陵": 7,
"安庆": 8,
"黄山": 10,
"滁州": 11,
"阜阳": 12,
"宿州": 13,
"巢湖": 14,
"六安": 15,
"亳州": 16,
"池州": 17,
"宣城": 18
}
},
"福建": {
"key": 35,
"defaultvalue": 1,
"values": {
"福州": 1,
"厦门": 2,
"莆田": 3,
"三明": 4,
"泉州": 5,
"漳州": 6,
"南平": 7,
"龙岩": 8,
"宁德": 9
}
},
"江西": {
"key": 36,
"defaultvalue": 1,
"values": {
"南昌": 1,
"景德镇": 2,
"萍乡": 3,
"九江": 4,
"新余": 5,
"鹰潭": 6,
"赣州": 7,
"吉安": 8,
"宜春": 9,
"抚州": 10,
"上饶": 11
}
},
"山东": {
"key": 37,
"defaultvalue": 1,
"values": {
"济南": 1,
"青岛": 2,
"淄博": 3,
"枣庄": 4,
"东营": 5,
"烟台": 6,
"潍坊": 7,
"济宁": 8,
"泰安": 9,
"威海": 10,
"日照": 11,
"莱芜": 12,
"临沂": 13,
"德州": 14,
"聊城": 15,
"滨州": 16,
"菏泽": 17
}
},
"河南": {
"key": 41,
"defaultvalue": 1,
"values": {
"郑州": 1,
"开封": 2,
"洛阳": 3,
"平顶山": 4,
"安阳": 5,
"鹤壁": 6,
"新乡": 7,
"焦作": 8,
"濮阳": 9,
"许昌": 10,
"漯河": 11,
"三门峡": 12,
"南阳": 13,
"商丘": 14,
"信阳": 15,
"周口": 16,
"驻马店": 17
}
},
"湖北": {
"key": 42,
"defaultvalue": 1,
"values": {
"武汉": 1,
"黄石": 2,
"十堰": 3,
"宜昌": 5,
"襄樊": 6,
"鄂州": 7,
"荆门": 8,
"孝感": 9,
"荆州": 10,
"黄冈": 11,
"咸宁": 12,
"随州": 13,
"恩施土家族苗族自治州": 28
}
},
"湖南": {
"key": 43,
"defaultvalue": 1,
"values": {
"长沙": 1,
"株洲": 2,
"湘潭": 3,
"衡阳": 4,
"邵阳": 5,
"岳阳": 6,
"常德": 7,
"张家界": 8,
"益阳": 9,
"郴州": 10,
"永州": 11,
"怀化": 12,
"娄底": 13,
"湘西土家族苗族自治州": 31
}
},
"广东": {
"key": 44,
"defaultvalue": 1,
"values": {
"广州": 1,
"韶关": 2,
"深圳": 3,
"珠海": 4,
"汕头": 5,
"佛山": 6,
"江门": 7,
"湛江": 8,
"茂名": 9,
"肇庆": 12,
"惠州": 13,
"梅州": 14,
"汕尾": 15,
"河源": 16,
"阳江": 17,
"清远": 18,
"东莞": 19,
"中山": 20,
"潮州": 51,
"揭阳": 52,
"云浮": 53
}
},
"广西": {
"key": 45,
"defaultvalue": 1,
"values": {
"南宁": 21,
"柳州": 22,
"桂林": 3,
"梧州": 4,
"北海": 5,
"防城港": 6,
"钦州": 7,
"贵港": 8,
"玉林": 9,
"百色": 10,
"贺州": 11,
"河池": 12
}
},
"海南": {
"key": 46,
"defaultvalue": 1,
"values": {
"海口": 1,
"三亚": 2,
"其他": 90
}
},
"重庆": {
"key": 50,
"defaultvalue": 1,
"values": {
"万州区": 1,
"涪陵区": 2,
"渝中区": 3,
"大渡口区": 4,
"江北区": 5,
"沙坪坝区": 6,
"九龙坡区": 7,
"南岸区": 8,
"北碚区": 9,
"万盛区": 10,
"双桥区": 11,
"渝北区": 12,
"巴南区": 13,
"黔江区": 14,
"长寿区": 15,
"綦江县": 22,
"潼南县": 23,
"铜梁县": 24,
"大足县": 25,
"荣昌县": 26,
"璧山县": 27,
"梁平县": 28,
"城口县": 29,
"丰都县": 30,
"垫江县": 31,
"武隆县": 32,
"忠县": 33,
"开县": 34,
"云阳县": 35,
"奉节县": 36,
"巫山县": 37,
"巫溪县": 38,
"石柱土家族自治县": 40,
"秀山土家族苗族自治县": 41,
"酉阳土家族苗族自治县": 42,
"彭水苗族土家族自治县": 43,
"江津市": 81,
"合川市": 82,
"永川区": 83,
"南川市": 84
}
},
"四川": {
"key": 51,
"defaultvalue": 1,
"values": {
"成都": 1,
"自贡": 3,
"攀枝花": 4,
"泸州": 5,
"德阳": 6,
"绵阳": 7,
"广元": 8,
"遂宁": 9,
"内江": 10,
"乐山": 11,
"南充": 13,
"眉山": 14,
"宜宾": 15,
"广安": 16,
"达州": 17,
"雅安": 18,
"巴中": 19,
"资阳": 20,
"阿坝": 32,
"甘孜": 33,
"凉山": 34
}
},
"贵州": {
"key": 52,
"defaultvalue": 1,
"values": {
"贵阳": 1,
"六盘水": 2,
"遵义": 3,
"安顺": 4,
"铜仁": 22,
"黔西南": 23,
"毕节": 24,
"黔东南": 26,
"黔南": 27
}
},
"云南": {
"key": 53,
"defaultvalue": 1,
"values": {
"昆明": 1,
"曲靖": 3,
"玉溪": 4,
"保山": 5,
"昭通": 6,
"楚雄": 23,
"红河": 25,
"文山": 26,
"思茅": 27,
"西双版纳": 28,
"大理": 29,
"德宏": 31,
"丽江": 32,
"怒江": 33,
"迪庆": 34,
"临沧": 35
}
},
"西藏": {
"key": 54,
"defaultvalue": 1,
"values": {
"拉萨": 1,
"昌都": 21,
"山南": 22,
"日喀则": 23,
"那曲": 24,
"阿里": 25,
"林芝": 26
}
},
"陕西": {
"key": 61,
"defaultvalue": 1,
"values": {
"西安": 1,
"铜川": 2,
"宝鸡": 3,
"咸阳": 4,
"渭南": 5,
"延安": 6,
"汉中": 7,
"榆林": 8,
"安康": 9,
"商洛": 10
}
},
"甘肃": {
"key": 62,
"defaultvalue": 1,
"values": {
"兰州": 1,
"嘉峪关": 2,
"金昌": 3,
"白银": 4,
"天水": 5,
"武威": 6,
"张掖": 7,
"平凉": 8,
"酒泉": 9,
"庆阳": 10,
"定西": 24,
"陇南": 26,
"临夏": 29,
"甘南": 30
}
},
"青海": {
"key": 63,
"defaultvalue": 1,
"values": {
"西宁": 1,
"海东": 21,
"海北": 22,
"黄南": 23,
"海南": 25,
"果洛": 26,
"玉树": 27,
"海西": 28
}
},
"宁夏": {
"key": 64,
"defaultvalue": 1,
"values": {
"银川": 1,
"石嘴山": 2,
"吴忠": 3,
"固原": 4
}
},
"新疆": {
"key": 65,
"defaultvalue": 1,
"values": {
"乌鲁木齐": 1,
"克拉玛依": 2,
"吐鲁番": 21,
"哈密": 22,
"昌吉": 23,
"博尔塔拉": 27,
"巴音郭楞": 28,
"阿克苏": 29,
"克孜勒苏": 30,
"喀什": 31,
"和田": 32,
"伊犁": 40,
"塔城": 42,
"阿勒泰": 43
}
},
"台湾": {
"key": 71,
"defaultvalue": 1,
"values": {
"台北": 1,
"高雄": 2,
"其他": 90
}
},
"香港": {
"key": 81,
"defaultvalue": 1,
"values": {
"香港": 1
}
},
"澳门": {
"key": 82,
"defaultvalue": 1,
"values": {
"澳门": 1
}
},
"海外": {
"key": 400,
"defaultvalue": 1,
"values": {
"美国": 1,
"英国": 2,
"法国": 3,
"俄罗斯": 4,
"加拿大": 5,
"巴西": 6,
"澳大利亚": 7,
"印尼": 8,
"泰国": 9,
"马来西亚": 10,
"新加坡": 11,
"菲律宾": 12,
"越南": 13,
"印度": 14,
"日本": 15,
"其他": 16
}
}
}';
print_r(json_decode($json,true));//第二个参数true表示强制转成数组
?>
注意事项:json_decode($json,true)中$json的编码必须是utf-8,不是的要用 iconv('GB2312', 'UTF-8', $json)转换;
json格式转数组注意事项的更多相关文章
- JSon_零基础_008_将JSon格式的"数组"字符串转换为List集合
将JSon格式的"数组"字符串转换为List集合. 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“name.values”集合, ...
- JSon_零基础_007_将JSon格式的"数组"字符串转换为Java对象"数组"
将JSon格式的"数组"字符串转换为Java对象"数组". 应用此技术从一个json对象字符串格式中得到一个java对应的对象. JSONObject是一个“n ...
- 如何解决jersey框架中以json格式返回数组,当数组中元素一个时json格式不对
原文地址:http://www.cnblogs.com/swpk/p/3566536.html?utm_source=tuicool jersey 是oracle 出的一个较好的REST框架.使用此框 ...
- ajax请求 json格式和数组格式总结
php echo json_encode($data); $.ajax({ url:APP+"?a=total&c=collection", //请求的页面 type:&q ...
- json格式的数组长度
var data = [{"id":"1","name":"a"},{"id":"2&qu ...
- c#迭代遍历带数组的json格式数据
[1]首先我们先创建一个带数组形式的json格式的数组 1)我们按照结构定义一个类,如下: using System;using System.Collections.Generic;using Sy ...
- json和php数组 格式的互相转换
$json_arr = array('WebName'=>'PHP网站开发教程网','WebSite'=>'http://www.jb51.net'); $php_json = json ...
- 使用CJSON库实现XML与JSON格式的相互转化
之前完成了一个两个平台对接的项目.由于这两个平台一个是使用json格式的数据,一个是使用xml格式的数据,要实现它们二者的对接就涉及到这两个数据格式的转化,在查阅相关资料的时候发现了这个CJSON库, ...
- Java对象与JSON互相转换jsonlib以及手动创建JSON对象与数组——(二)
首先声明一下,jsonlib转换与GSON相比太差劲了,操作不是一般的繁琐.GSON可以直接转换成各种集合与对象类型.强烈推荐使用GSON.而且GSON一个方法就可以解决,jsonlib转来转去太繁琐 ...
随机推荐
- Linux的五个查找命令:find,locate,whereis,which,type 及其区别
1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...
- tiny4412 串口驱动分析一 --- u-boot中的串口驱动
作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...
- ini配置文件在LINUX下面程序打不开?
ini配置文件在LINUX下面程序打不开? 在WINDOWS里面编辑的INI配置文件保存时默认是ANSI字符编码,LINUX可能不识别,导致LINUX程序不能打开INI配置文件,成功读取里面的参数. ...
- windows和linux之间上传下载文件的两种方法
一 用rz和sz http://blog.csdn.net/register_man/article/details/53860774 http://www.jb51.net/article/7 ...
- C# HttpWebRequest 绝技 【转】
原文地址:http://www.sufeinet.com/thread-6-1-1.html 在线测试工具http://www.sufeinet.com/thread-3690-1-1.html c# ...
- 理解支持向量机(四)LibSVM工具包的使用
LibSVM是一款简单易用的支持向量机工具包.包括了C和Java的开发源代码.大家能够訪问其官网进行了解和下载相关文件. 这里以其官网的第一个数据集a1a 为例.练习使用多项式核和径向基核来对数据集进 ...
- [PWA] Add Push Notifications to a PWA with React in Chrome and on Android
On Android and in Chrome (but not on iOS), it's possible to send push notifications with a PWA. We'l ...
- Linux Ubuntu下Dropbox图标消失
Linux下的Dropbox是支持命令行模式的.,在terminal中输入dropbox后,如下提示: Dropbox command-line interface commands: Note: u ...
- 微信小程序 - 关于下拉刷新
// 拉取数据 fetchData: function() { wx.request({ url: 'http://v.juhe.cn/toutiao/index', data: { type: '' ...
- Android控件常见属性
1.宽/高android:layout_width android:layout_height// 取值match_parent //匹配父控件wrap_content //自适应,根据内容 如果指定 ...