[PHP] replaces the JSON model's data with JSON data, how to resolve

Source: Internet
Author: User
[PHP] replaces JSON model data with JSON data
{data:[{code: "Spot gold", Newprice: "1662.3", Hight: "0", Low: "0", Buy: "1662.3", Sell: "r1663.3", Zhang: "r4.75", Date: " 4:59:40 ", Open:" 0 ", Close:" 1657.55 "}, {code:" Hong Kong Gold ", Newprice:" 15384 ", Hight:" 0 ", Low:" 0 ", Buy:" 15384 ", Sell:" r15390 ", Zhang: "R44", Date: "2:59:55", Open: "0", Close: "15340"}, {code: "Spot Silver", Newprice: "31.25", Hight: "0", Low: "0", Buy: "31.25" , Sell: "r31.31", Zhang: "r0.17", Date: "4:56:10", Open: "0", Close: "31.08"}, {code: "Spot Platinum", Newprice: "1566.75", Hight: "0", Low: "0", Buy: "1566.75", Sell: "r1576.75", Zhang: "r2.01", Date: "3:29:35", Open: "0", Close: "1564.74"}, {code: "Spot Palladium", Newprice: "677.23", Hight: "0", Low: "0", Buy: "677.23", Sell: "r684.23", Zhang: "r9.54", Date: "3:29:35", Open: "0", Close: " 667.69 "}]}


Everyone, this is a model with a JSON data, but the key name does not have quotation marks, so Json_decode will consider it an illegal JSON data format. Now use the regular to replace it with quotation marks.

/(? <={|,) [^:\s]*/is, currently can match a two-dimensional data, but replace the words, no, it must be a one-dimensional array.

Ask you heroes.

------Solution--------------------
PHP Code
  ------Solution--------------------
$json =preg_replace ('/(, |,?\s*?{) \b (\w+):/', ' $ "$": ', $json);
Print_r (Json_decode ($json, true));

------Solution--------------------
or the Boss Mighty
2 Floor modification is ok ({|,) ([^:\s]*) (? =:) discuss

Upstairs, very good, is the result I want.

Would you like to ask: with regular (? <={|,) [^:\s]* (? =:) this way, no??

------Solution--------------------
discuss

Upstairs, very good, is the result I want.

Would you like to ask: with regular (? <={|,) [^:\s]* (? =:) this way, no??
  • 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.