如何使用PHP以json格式获取Deeplinks?

时间:2017-04-04 17:44:52

标签: php json

如何使用PHP获取JSON格式的Deeplinks?

这是我实际拥有的JSON对象

    $i=1;       
    foreach($data->webPages->value as $val){
        if($i==1){
            $part   = $val['deepLinks']->snippet;
        }   
    $i++;       
    }

这是我到目前为止所做的代码:

Fatal error: Cannot use object of type stdClass as array.

结果:@testable import YOUR_MODULE_NAME class MyViewControllerTests: XCTestCase { var myVC : MyViewController! }

如何获得该值?

2 个答案:

答案 0 :(得分:0)

我认为这可能与$ data有关。它不是数组。

您是否使用json_decode()将JSON对象转换为PHP变量?如果是,那么您需要将其更改为:

 json_decode($myJSON, true);

如果设置为true,PHP将返回一个关联数组,但如果不是PHP,它将返回一个对象(stdClass)。

您可以详细了解原因here

答案 1 :(得分:0)

如果你想要一个平面的深度链接数组,试试这个:

$ret = [];
foreach($data->webPages->value as $val){
    if(property_exists($val,"deepLinks")){
        $ret = array_merge($ret, $val->deepLinks);
    }
}
var_dump($ret);

我测试了它并且有效。

另一方面,如果你想把它分组:

$ret = [];
foreach($data->webPages->value as $val){
    if(property_exists($val,"deepLinks")){
        $ret[] = $val->deepLinks;
    }
}
var_dump($ret);

我使用的测试JSON:

$data = '{
    "_type": "SearchResponse",
    "webPages": {
        "webSearchUrl": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=sFk5GtkV8PxaqVq0K3GniaQOQJqKR2jqw1udftLC9QE&v=1&r=https%3a%2f%2fwww.bing.com%2fsearch%3fq%3dcontemporary%2bfurniture&p=DevEx,5336.1",
        "totalEstimatedMatches": 46000000,
        "value": [{
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.0",
            "name": "Contemporary Furniture. Modern and Scandinavian designs.",
            "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=Q0wwocl-JR44YrK1RsR_5rSqO4KPlSPx0KgUi-sMocA&v=1&r=https%3a%2f%2fcontemporaryfurniture.com%2f&p=DevEx,5080.1",
            "displayUrl": "https:\/\/contemporaryfurniture.com",
            "snippet": "The finest in contemporary and modern furniture. Imported and domestic styles, bedrooms, sofas, home office, more. Shop online or visit our NY showroom.",
            "deepLinks": [{
                "name": "Bedrooms",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=jo1rIVengiXnaKOkFczZ54qrp9-L4W2jb1sJ41jl04g&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fbedrooms-19%2f&p=DevEx,5072.1",
                "snippet": "Bedroom sets including beds, dressers, nightstands and chests. Produced in various woods and lacquers. Contemporary, scandinavian and modern designs."
            }, {
                "name": "Dining Rooms",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=EsyLPbUccu5BzwCeceP_S5u46vo4gSDMG7vP29BfjFE&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fdining-rooms-12%2f&p=DevEx,5073.1",
                "snippet": "Dining Room sets, dining tables, servers, buffet and china cabinets. Contemporary and Scandinavian styles."
            }, {
                "name": "Barstool",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=6rOwsJH4uT4LzjG833eSRYQgDMCKiogHGdgnokFVSEg&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fbarstool-16%2f&p=DevEx,5074.1",
                "snippet": "Barstools and counterstools contemporary, modern and scandinavian designs produced in wood and metal."
            }, {
                "name": "Chairs",
                "url": "https:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=ADlw0i43aQhdLC0tHBfjMFjYwiWYHZ3ggdG6RhZv9ss&v=1&r=https%3a%2f%2fwww.contemporaryfurniture.com%2fshop%2fchairs-13%2f&p=DevEx,5075.1",
                "snippet": "Chairs to compliment your living room produced in fabric upholstery and leather. Contemporary and modern designs."
            }],
            "dateLastCrawled": "2017-03-31T17:25:00"
        }, {
            "id": "https:\/\/api.cognitive.microsoft.com\/api\/v5\/#WebPages.1",
            "name": "Contemporary Furniture | Houzz",
            "url": "http:\/\/www.bing.com\/cr?IG=B470EF0A41D94B338002992668141CC5&CID=1B1D0110A94966F5383E0B4AA8A067DE&rd=1&h=vsM0va_8kehN5ZnPil2frs3eKL16FOHnut88FvEHRsM&v=1&r=http%3a%2f%2fwww.houzz.com%2fphotos%2fcontemporary%2ffurniture&p=DevEx,5098.1",
            "displayUrl": "www.houzz.com\/photos\/contemporary\/furniture",
            "snippet": "Browse a wide selection of contemporary furniture for sale on Houzz, including wood furniture and leather furniture for your dining room, office, bedroom and living room.",
            "dateLastCrawled": "2017-03-30T22:51:00"
        }]
    }
}';

我完成了缺失的部分。