Php reads and parses json files

Source: Internet
Author: User
Php reads and parses json files
 
 "; $ Json2_string = '{" tsnpm ": {" name ":" # Special Steak noodles # "," score ":" (4.7 points) "," price ": "¥9.0", "description": "The competition is vigorous and refreshing. "}," Mnnrm ": {" name ":" #: Minnan beef noodle # "," score ":" (4.8 points) "," price ":" ¥8.5 ", "description": "smooth and delicious, with an endless aftertaste. "}," Hxncm ": {" name ":" # braised beef noodles # "," score ":" (4.8 points) "," price ":" ¥8.0 ", "description": "It's very attractive! "}," Gwcsgjf ": {" name ":" # wide-flavor barbecued pork cover rice # "," score ":" (4.7 points) "," price ":" ¥8.5 ", "description": "nourishing, nourishing, delicious, energetic"}, "xhscd": {"name": "# tomato scrambled eggs #", "score": "(4.8 points) "," price ":" ¥2.0 "," description ":" tastes nice and inexpensive! "} '; $ Obj = json_decode ($ json2_string, true); if (! Is_array ($ obj) die ('No successful '); print_r ($ obj); echo'
'. $ Obj ['tsnpm'] ['name'];?>

When I parse the $ json2_string string, it can be successfully parsed, but I cannot parse the string $ json_string obtained from the file. how can I solve this problem?


Reply to discussion (solution)

Is your json file in UTF-8 format with BOM header? Remove the BOM header.

Which is $ json2_string? paste them separately.

./Js/recommendMenu. json can be opened in the form of a connection?

Echo print_r ($ json_string, true );
Only print the read string

I didn't see the json_decode ($ json_string) in your code. how do you know that it cannot be parsed?

$ Obj = json_decode ($ json2_string, true );
If (! Is_array ($ obj) die ('No successful ');

Are you prompted?

Is your json file in UTF-8 format with BOM header? Remove the BOM header.


The first floor is correct. my code is in the UTF-8 format of the BOM header, and the same as the string is removed.

./Js/recommendMenu. json can be opened in the form of a connection?


Some are in the 39th rows

I am using a wrong code. thank you for your enthusiastic reply.

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.