micky007 Posted February 18, 2019 Share Posted February 18, 2019 Hi Guys, One of my API providers has recently updated the way they output data in JSON to the following: { "ua":"Mozilla\/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko\/20100101 Firefox\/65.0", "type":"browser", "brand":null, "name":null, "url":"https:\/\/www.mozilla.org\/", "os":{ "name":"Windows 10", "code":"windows_10", "url":"https:\/\/en.wikipedia.org\/wiki\/Windows_10", "family":"Windows", "family_code":"windows", "family_vendor":"Microsoft Corporation.", "icon":"https:\/\/assets.userstack.com\/icon\/os\/windows10.png", "icon_large":"https:\/\/assets.userstack.com\/icon\/os\/windows10_big.png" }, "device":{ "is_mobile_device":false, "type":"desktop", "brand":null, "brand_code":null, "brand_url":null, "name":null }, "browser":{ "name":"Firefox", "version":"65.0", "version_major":"65", "engine":"Gecko" }, "crawler":{ "is_crawler":false, "category":null, "last_seen":null } } Previous the JSON data was not nested which means i now need to update my code to grab the values, I cant work out how to grab nested values, below is my code working off the old JSON output. How do i go about grabbing the data now with the new format? $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "URL-REMOVED"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result = curl_exec($ch); $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $apiresult = json_decode($result); foreach($apiresult as $result) { $ua_type = $result->ua_type; $os_name = $result->os_name; $os_version = $result->os_version; $browser_name = $result->browser_name; $browser_version = $result->browser_version; $ua_brand = $result->ua_brand; $ua_name = $result->ua_name; } Quote Link to comment https://forums.phpfreaks.com/topic/308352-php-json-nested/ Share on other sites More sharing options...
micky007 Posted February 18, 2019 Author Share Posted February 18, 2019 Its ok guys, this can be marked as resolved. I've decided to re-code my work to make it better. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE ); $json = curl_exec( $ch ); curl_close( $ch ); $api_result = json_decode( $json, true ); $ua_type = $api_result[ 'device' ][ 'type' ]; $os_name = $api_result[ 'os' ][ 'family' ]; $os_version = $api_result[ 'os' ][ 'name' ]; $browser_name = $api_result[ 'browser' ][ 'name' ]; $browser_version = $api_result[ 'browser' ][ 'version' ]; $ua_brand = $api_result[ 'device' ][ 'brand' ]; $ua_name = $api_result[ 'device' ][ 'name' ]; Quote Link to comment https://forums.phpfreaks.com/topic/308352-php-json-nested/#findComment-1564567 Share on other sites More sharing options...
requinix Posted February 18, 2019 Share Posted February 18, 2019 If you liked using objects more and just weren't sure how to, it goes like $ua_type = $api_result->device->type; Looks a lot like the array syntax, right? Quote Link to comment https://forums.phpfreaks.com/topic/308352-php-json-nested/#findComment-1564609 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.