首页 > 代码库 > json格式转数组注意事项

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表示强制转成数组
?>