[{id:'150',cnname:'福州市'},{id:'151',cnname:'厦门市'},{id:'152',cnname:'莆田市'},{id:'153',cnname:'三明市'},{id:'154',cnname:'泉州市'},{id:'155',cnname:'漳州市'},{id:'156',cnname:'南平市'},{id:'157',cnname:'龙岩市'},{id:'158',cnname:'宁德市'}]
是格式问题吗,我把所有的字段都用单引号包含起来,还是json_decode($str_json, 1);无法转成数组
[{id:'150',cnname:'福州市'},{id:'151',cnname:'厦门市'},{id:'152',cnname:'莆田市'},{id:'153',cnname:'三明市'},{id:'154',cnname:'泉州市'},{id:'155',cnname:'漳州市'},{id:'156',cnname:'南平市'},{id:'157',cnname:'龙岩市'},{id:'158',cnname:'宁德市'}]
是格式问题吗,我把所有的字段都用单引号包含起来,还是json_decode($str_json, 1);无法转成数组
看看 JSON 的语法定义,然后你会发现,其实 JSON 语法中并没有单引号,只有双引号,而且所有键都是字符串表示,也就是说,是必须带引号的。
你这段内容是合法的 JS 数组表示,但不是 JSON 格式。
<code class="json">[ { "id": "150", "cnname": "福州市" }, { "id": "151", "cnname": "厦门市" }, { "id": "152", "cnname": "莆田市" }, { "id": "153", "cnname": "三明市" }, { "id": "154", "cnname": "泉州市" }, { "id": "155", "cnname": "漳州市" }, { "id": "156", "cnname": "南平市" }, { "id": "157", "cnname": "龙岩市" }, { "id": "158", "cnname": "宁德市" } ]</code>