Home > Article > Backend Development > Why can’t the following json be converted into an array normally?
[{id:'150',cnname:'Fuzhou City'},{id:'151',cnname:'Xiamen City'},{id:'152',cnname:'Putian City'},{id: '153',cnname:'Sanming City'},{id:'154',cnname:'Quanzhou City'},{id:'155',cnname:'Zhangzhou City'},{id:'156',cnname :'Nanping City'},{id:'157',cnname:'Longyan City'},{id:'158',cnname:'Ningde City'}]
Is it a format problem? I include all fields in single quotes, or is json_decode($str_json, 1); unable to be converted into an array
[{id:'150',cnname:'Fuzhou City'},{id:'151',cnname:'Xiamen City'},{id:'152',cnname:'Putian City'},{id: '153',cnname:'Sanming City'},{id:'154',cnname:'Quanzhou City'},{id:'155',cnname:'Zhangzhou City'},{id:'156',cnname :'Nanping City'},{id:'157',cnname:'Longyan City'},{id:'158',cnname:'Ningde City'}]
Is it a format problem? I include all fields in single quotes, or is json_decode($str_json, 1); unable to be converted into an array
Look at the syntax definition of JSON, and you will find that there are actually no single quotes in JSON syntax, only double quotes, and all keys are represented by strings, which means they must be quoted.
Your content is a legal JS array representation, but it is not in JSON format.
<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>