简体   繁体   中英

get value from json file via php non array

I'm a beginner in json please help
I'm trying to access value of certain objects from an online published json file via php script and not able to do so following the examples from this forum

<?php
$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primary topic"]["CompanyName"];
print $companyname;
?>

i get the following error

( ! ) Notice: Undefined index: primary topic in C:\wamp\www\json.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0000  244456  {main}( )   ..\json.php:0

I have tried single and double quotes, [0] for array but to no avail

You should have to use primaryTopic :

$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primaryTopic"]["CompanyName"];
print $companyname;

Output will : ZENITH PRINT (UK) LIMITED

I think you had a mistake at 'primary topic' key. The key-name i saw in the response is 'primaryTopic'. Could you please check again?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM