PHP parsing JSON

Source: Internet
Author: User
Tags preg
This post was last edited by Yskang on 2012-08-08 10:07:31

This is a software on-line situation
Remotely obtained JSON data, where I need records value, this records:31 is the total number of online, and then get 14041221001, Nong Hasi Pond |14041220001, Nong Ha Tung Zhang, these values write into their own database, If the first 14041221001 is the account number of Nong Hasi Pond. This format is how to parse.

{total:1,page:1,records:31,rows:[{id: ' 14041221001 ', cell:["1", "14041221001", "Nong Hasi Tong"]},{id: ' 14041220001 ', cell:["2 "," 14041220001 "," Nong Ha Dong Zhang "]},{id: ' 14041206001 ', cell:[" 3 "," 14041206001 "," Nong Hachenwu "]},{id: ' 14041205001 ', cell:[" 4 "," 14041205001 "," Agriculture Kazakhstan Zhao Zhuang "]},{id: ' 14041202001 ', cell:[" 5 "," 14041202001 "," Agricultural Kazakhstan North Xing "]},{id: ' 14041201001 ', cell:[" 6 "," 14041201001 "," Nong Ha da Cao "]},{id: ' 14041129001 ', cell:[" 7 "," 14041129001 "," Agricultural Olympiad full Zhuang "]},{id: ' 14041109001 ', cell:[" 8 "," 14041109001 "," Qiao Zhuang "]},{id: ' 14041101001 ', cell:[" 9 "," 14041101001 "," Agri-Olympic Store Center "]},{id: ' 14041010001 ', cell:[" 10 "," 14041010001 "," Ruro Black and White "]},{id: ' 14041009001 ', cell:[" 11 "," 14041009001 "," Ruro da Lup "]},{id: ' 14041006001 ', cell:[" 12 "," 14041006001 "," Ruro car Lee "]},{id: ' 14040912001 ', cell:[" 13 "," 14040912001 "," Enshi Nine Kiln "]},{id: ' 14040904001 ', cell:[' 14 '," 14040904001 "," Enshi cabbage wei "]},{id: ' 14040704001 ', cell:[" 15 "," 14040704001 "," Shibuya Pay Set "]},{id: ' 14040617001 ', cell:[" 16 "," 14040617001 "," torch three Yuan "]},{id: ' 14040602001 ', cell:[" 17 "," 14040602001 "," Torch Yang Zhuang "]},{id: ' 140406001 ', cell:[" 18 "," 140406001 "," Torch Office "]},{id: ' 14040530001 ', cell:[" 19 "," 14040530001 "," Kaegu "]},{id: ' 14040523001 ', cell:[" 20 "," 14040523001 "," Add Love Ding Zhuang "]},{id: ' 14040520001 ', cell:[" 21 "," 14040520001 "," Gaeweian "]},{id: ' 14040509001 ', cell:[" 22 "," 14040509001 "," Plus love Gao su "]},{id: ' 14040449001 ', cell:[" 23 "," 14040449001 "," Settle Colpitts Liu " ]},{id: ' 14040444001 ', cell:["24", "14040444001", "Home Wangzhuang"]},{id: ' 14040422001 ', cell:["25", "14040422001", "An Jiameng set"]},{id : ' 14040319001 ', cell:["26", "14040319001", "Sheva Wang Zhuang cun"]},{id: ' 14040128001 ', cell:["27", "14040128001", "Robe Hassuchen"]},{id: ' 14040127001 ', cell:["28", "14040127001", "robe and shop"]},{id: ' 14040123001 ', cell:[' 29 ', ' 14040123001 ', ' robe Hachenwan hou ']},{id: ' 14040119001 ', cell:["30", "14040119001", "robe Haesche Set"]},{id: ' 1404002 ', cell:["31", "1404002", "County Office"}]}

Is he the standard JSON, I see the JSON is quoted in quotation marks, he this many did not cause.


Reply to discussion (solution)

You directly use Json_decode ($string, true); Give it a try

It's JSON, it works.

How to write Ah, I also know json_decode, but always wrong

This is the data block for JS, not the JSON used for data interchange. At least he can't be accepted by PHP Json_decode.

Set data to be stored in the variable $s
$s = preg_replace ('/[' \ ')? ( \w+) ["\ ']?/', '" $ "', $s);
$s = iconv (' GBK ', ' utf-8 ', $s);//If it is not utf-8 encoded, it must be converted to Utf-8 code
Print_r (Json_decode ($s));

This is the data block for JS, not the JSON used for data interchange. At least he can't be accepted by PHP Json_decode.

Set data to be stored in the variable $s
$s = preg_replace ('/[' \ ')? ( \w+) ["\ ']?/', '" $ "', $s);
$s = iconv (' GBK ', ' utf-8 ', $s);//If it is not utf-8 encoded, it must be converted to Utf-8 code
Pri......
What's $

Http://www.yesky.com/imagesnew/software/php/zh/function.preg-replace.html

Http://www.yesky.com/imagesnew/software/php/zh/function.preg-replace.html
Seeking QQ

I don't know why Json_decode can't resolve the master.

  • 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.