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