0

我有一些我想使用 PHP 解析的 json 数据。

我想提取:

ID、姓名、DESC、地址(完整)、电话和营业时间。

我努力了。

$json = json_encode( $value, true );
foreach($json['results'] as $result) {
echo 'Title: ' . $result['general']['name'] . '<br />';
}

但出现以下错误:

PHP Warning:  Illegal string offset 'results' in ...
PHP Warning:  Invalid argument supplied for foreach() in ....

这是我要解析的 JSON 数据。

"ok":true,
"query":"decarli",
"page":0,
"count":100,
"total":1,
"time":"2013-01-31 00:57:40",
"results":[
{
"id":"decarli-restaurant",
"factualId":"f8990729-3ae7-4efa-8037-38fa899b07ea",
"outOfBusiness":false,
"publishedAt":"2012-03-09 10:35:51",
"general":{
"name":"Decarli Restaurant",
"timeZone":"EST",
"desc":"Decarli is an ambitious restaurant created by Jana and Paul Decarli, a husband and wife team who along with their professional staff, are dedicated to food, wine, and providing an excellent dining experience.\r\n\r\nPaul, a longtime Oregonian and a graduate of Western Culinary Institute, developed his expertise at many of Portland\u00e2\u20ac\u2122s top restaurants including Paragon, Fratelli, Saucebox, and Tuscany Grill where he was Executive Chef.\r\n\r\nJana, an Oregon native and University of Oregon graduate, established her outstanding service skills and standards at San Francisco\u00e2\u20ac\u2122s esteemed 42 Degrees and Portland\u00e2\u20ac\u2122s Bluehour. \r\n\r\nDecarli is committed to showcasing the state\u00e2\u20ac\u2122s considerable bounty of seasonal ingredients. Paul\u00e2\u20ac\u2122s culinary style draws on his Swiss\/Italian-American roots to create food that is at once sophisticated and rustic.\r\n\r\nThe restaurant\u00e2\u20ac\u2122s interior reflects the couple\u00e2\u20ac\u2122s approach to food and wine\u00e2\u20ac\u201dwarm, inviting, thoughtful and authentic. The 4000 square foot space features restored Douglas Fir floors, exposed brick walls, and a soaring beam and rafters ceiling punctuated with skylights. A highlight of the dining room is a large brass chandelier salvaged from downtown Portland\u00e2\u20ac\u2122s historic Benson Hotel. An open kitchen overlooks the comfortable bar area.\r\n\r\nWe invite you to join us soon.",
"website":"http:\/\/decarlirestaurant.com"
},
"location":{
"address1":"4545 SW Watson Ave",
"address2":"",
"city":"Beaverton",
"region":"OR",
"country":"US",
"postcode":"97005",
"longitude":-122.80611,
"latitude":45.48648
},
"phones":{
"main":"503-641-3223",
"fax":null
},
"hours":{
"mon":{
"start":"closed",
"end":"closed"
},
"tue":{
"start":"04:30pm",
"end":"10:00pm"
},
"wed":{
"start":"04:30pm",
"end":"10:00pm"
},
"thu":{
"start":"04:30pm",
"end":"10:00pm"
},
"fri":{
"start":"04:30pm",
"end":"11:00pm"
},
"sat":{
"start":"04:30pm",
"end":"11:00pm"
},
"sun":{
"start":"05:00pm",
"end":"09:00pm"
},
"holidaySchedule":""
},
"businessType":"Restaurant"
}
]
}
4

1 回答 1

2
$json = json_decode( $value, true );
if (isset($json['results']) && is_array($json['results']) ) {
        foreach($json['results'] as $result) {
              echo 'Title: ' . $result['general']['name'] . '<br />';
        }
}
else echo "\$json['results'] is not an array";
于 2013-01-31T07:06:27.520 回答