dongpao1083 2013-01-31 06:59
浏览 60

使用PHP脚本解析JSON数据字符串

I have some json data that I would like to parse using PHP.

I would like to extract:

ID,NAME,DESC,ADDRESS (full),PHONE and HOURS.

I have tried.

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

but getting following error:

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

Here is the JSON data that I want to parse.

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

Paul, 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.

Jana, 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. 

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

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

We 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"
}
]
}
  • 写回答

1条回答 默认 最新

  • download1002 2013-01-31 07:06
    关注
    $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";
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!