Inadvertently found that PHP Json_decode has a bug

Source: Internet
Author: User
Tags vars

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?
  1. <?php
  2. $string = ' {' SKUs ': [
  3. {
  4. "Shadename": null,
  5. "Hex_value_string": "",
  6. "sku_base_id": 5912,
  7. "Priceplustax": 8400,
  8. "Product_size": "220g",
  9. "Price": 8000,
  10. "sku_id": "SKU5912",
  11. "Inventory_status": 2,
  12. "Product_code": 016500
  13. }
  14. ],
  15. "Shaded": 0,
  16. "Prod_cat_image_name": 0165,
  17. "Skintype_desc": "",
  18. "product_id": "PROD2158"
  19. }
  20. ‘;
  21. The following regular is the conversion of 0615 such data to "0615"
  22. $pattern = "/:(\s*) 0 (\d+)/ui";
  23. $replacement = ': \\1 ' 0\\2 ';
  24. $string = Preg_replace ($pattern, $replacement, $string);
  25. $result = Json_decode ($string, true);
  26. Print_r ($result);
  27. $string = Json_encode ($result);
  28. echo $string;
  29. ?>

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

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.