One, what is JSON
JSON is a data structure that is easy to read and write, and is easy for machine parsing and generation.
JSON constructs two types of structures:
1, "name/value" pairs of sets, in different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or an associative array (associative array).
2, the value of the ordered list (an ordered list of values). In most languages, it is understood as an array (array).
The JSON has these forms:
An object is an unordered collection of "name/value pairs". An object starts with "{" (opening parenthesis) and "}" (the closing parenthesis) ends. Each "name" followed by a ":" (colon); "' Name/value ' pair", "(comma) delimited
Two, the problem I encountered
Look at the following example:
View copy print?
- <?php
- $string = ' {' SKUs ': [
- {
- "Shadename": null,
- "Hex_value_string": "",
- "sku_base_id": 5912,
- "Priceplustax": 8400,
- "Product_size": "220g",
- "Price": 8000,
- "sku_id": "SKU5912",
- "Inventory_status": 2,
- "Product_code": 016500
- }
- ],
- "Shaded": 0,
- "Prod_cat_image_name": 0165,
- "Skintype_desc": "",
- "product_id": "PROD2158"
- }
- ‘;
- The following regular is the conversion of 0615 such data to "0615"
- $pattern = "/:(\s*) 0 (\d+)/ui";
- $replacement = ': \\1 ' 0\\2 ';
- $string = Preg_replace ($pattern, $replacement, $string);
- $result = Json_decode ($string, true);
- Print_r ($result);
- $string = Json_encode ($result);
- echo $string;
- ?>
Operation and Analysis:
1, if I take out the regular thing, Print_r ($result); it doesn't show anything at all, the problem is that on 016500 such data, he doesn't know whether such data is a string type or a numeric type. Plus double quotes is fine.
2, I $string the array again = Json_encode ($result), and printed the JSON string, found that in the Json_encode will put 016500 double quotation marks, it can be concluded that the PHP code in the JSON string is generated manually.
3, even if the data like 016500 does not add double quotes, I think Json_decode can also make accurate judgments, because this logic is not complex, so I think json_decode should be improved, And I use PHP version is 5.2.3, do not know the latest version has not fixed this bug
Reference: http://blog.51yip.com/php/934.html
Inadvertently found that PHP Json_decode has a bug