[PHP] how to replace the JSON data with JSON data

Source: Internet
Author: User
[PHP] replace the JSON model data with JSON data {data: [{code: & quot; spot gold & quot;, newPrice: & quot; 1662.3 & quot ;, hight: & quot; 0 & quot;, low: & quot; 0 & quot;, buy: & quot; 1662.3 & quot;, weight: & quot; r1663.3 & quot ;, zhang: & qu [PHP] replaces the JSON data with the JSON data.
{Data: [{code: "Spot Gold", newPrice: "1662.3", hight: "0", low: "0", buy: "1662.3", weight: "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", dimensions: "r15390", zhang: "r44", date: "2:59:55 ", open: "0", close: "15340" },{ code: "Spot silver", newPrice: "31.25", hight: "0", low: "0 ", buy: "31.25", clerk: "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", priority: "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", priority: "r684.23", zhang: "r9.54", date: "3:29:35", open: "0 ", close: "667.69"}]}


As you can see, this is a JSON data model, but the key name is not enclosed by quotation marks. Therefore, json_decode will regard it as an invalid JSON data format. Replace it with a regular expression and enclose it with quotation marks.

/(? <= {|,) [^: \ S] */is. Currently, a two-dimensional data can be matched. However, if replace is used, it cannot be used. it must be a one-dimensional array.

Ask heroes.

------ Solution --------------------
PHP code
  ------ Solution --------------------
$ Json = preg_replace ('/(, | ,? \ S *? {) \ B (\ w +):/',' $1 "$2": ', $ json );
Print_r (json_decode ($ json, true ));

------ Solution --------------------
Or the boss Wei Wu
You can modify ({|,) ([^: \ s] *) (? = :) Discussion

Upstairs, great. it's the result I want.

I would like to ask: use regular expressions (? <= {|,) [^: \ S] * (? = :) Is that true ??

------ Solution --------------------
Discussion

Upstairs, great. it's the result I want.

I would like to ask: use regular expressions (? <= {|,) [^: \ S] * (? = :) Is that true ??

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.