[{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>