If php_encode is used in the PHP array and JSON. parse is used to convert it into a js object, an error is returned (the previous value in the array is a json string)

Source: Internet
Author: User
Array: {code ...} json: {code ...} pics in the array is json, and then json_encode is followed by JSON in js. parse reports the following error: {code ...} array:

Array ([0] => Array ([productid] => 2 [cateid] => 4 [title] => clothing [descr] => [num] => 197 [price] => 888.00 [cover] => 7xpizy.com1.z0.glb.clouddn.com/58087fa893aa7 [pics] => {"58087faa67f8b ": "7xpizy.com1.z0.glb.clouddn.com \/58087faa67f8b"} [issale] => 1 [saleprice] => 799.00 [ishot] => 1 [istui] => 1 [ison] => 1 [createtime] => 0 ))

Json:

[{"productid":"2","cateid":"4","title":"\u8863","descr":"\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416","num":"197","price":"888.00","cover":"7xpizy.com1.z0.glb.clouddn.com\/58087fa893aa7","pics":"{\"58087faa67f8b\":\"7xpizy.com1.z0.glb.clouddn.com\\\/58087faa67f8b\"}","issale":"1","saleprice":"799.00","ishot":"1","istui":"0","ison":"1","createtime":"0"}]

Pics in the array is json, and thenjson_encodeLater in jsJSON.parseAn error is reported:

Unexpected number in JSON at position 169

Reply content:

Array:

Array ([0] => Array ([productid] => 2 [cateid] => 4 [title] => clothing [descr] => [num] => 197 [price] => 888.00 [cover] => 7xpizy.com1.z0.glb.clouddn.com/58087fa893aa7 [pics] => {"58087faa67f8b ": "7xpizy.com1.z0.glb.clouddn.com \/58087faa67f8b"} [issale] => 1 [saleprice] => 799.00 [ishot] => 1 [istui] => 1 [ison] => 1 [createtime] => 0 ))

Json:

[{"productid":"2","cateid":"4","title":"\u8863","descr":"\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416","num":"197","price":"888.00","cover":"7xpizy.com1.z0.glb.clouddn.com\/58087fa893aa7","pics":"{\"58087faa67f8b\":\"7xpizy.com1.z0.glb.clouddn.com\\\/58087faa67f8b\"}","issale":"1","saleprice":"799.00","ishot":"1","istui":"0","ison":"1","createtime":"0"}]

Pics in the array is json, and thenjson_encodeLater in jsJSON.parseAn error is reported:

Unexpected number in JSON at position 169

Process the original array, and convert the json part into an array

$ Arr = Array ("productid" => 2, "cateid" => 4, "title" => "clothing ", "descr" => "", "num" => 197, "price" => 888.00, "cover" => "shield", "pics" => '{"58087faa67f8b": "7xpizy.com1.z0.glb.clouddn.com \/58087faa67f8b"}', "issale" => 1, "saleprice" => 799.00, "ishot" => 1, "istui" => 1, "ison" => 1, "createtime" => 0 )); foreach ($ arr as $ k => $ v) {$ arr [$ k] ['Pics '] = array (json_decode ($ arr [$ k] ['Pics'], true ));} echo json_encode ($ arr );
[{"productid":2,"cateid":4,"title":"\u8863","descr":"\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416\u5416","num":197,"price":888,"cover":"7xpizy.com1.z0.glb.clouddn.com\/58087fa893aa7","pics":[{"58087faa67f8b":"7xpizy.com1.z0.glb.clouddn.com\/58087faa67f8b"}],"issale":1,"saleprice":799,"ishot":1,"istui":1,"ison":1,"createtime":0}]

$ Arr = [['produtid' => 2, 'cateid' => 4, 'title' => 'yie ', 'descal' => ' ', 'num' => 197, 'price' => 888.00, 'cover' => '7xpizy finished, 'pics '=>' {"58087faa67f8b": "Failed \/58087faa67f8b"} ', 'issale' => 1, 'saleprice' => 799.00, 'ishot '=> 1, 'istu' => 1, 'ison' => 1, 'createtime' => 0,]; $ arr [0] ['Pics '] = json_decode ($ arr [0] ['Pics'], true); $ json = json_encode ($ arr ); // The following is the normal output var json = 'of js'
  '; Console. log (JSON. parse (json ));
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.