I am trying to access the value for one of the currencies (e.g. GBP) within the "rates" object of the following JSON file:
JSON file:
{
"success":true,
"timestamp":1430594775,
"rates":{
"AUD":1.273862,
"CAD":1.215036,
"CHF":0.932539,
"CNY":6.186694,
"EUR":0.893003,
"GBP":0.66046,
"HKD":7.751997,
"JPY":120.1098,
"SGD":1.329717
}
}
This was my approach:
PHP (CURL):
$url = ...
// initialize CURL:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// get the (still encoded) JSON data:
$json = curl_exec($ch);
curl_close($ch);
// store decoded JSON Response in array
$exchangeRates = (array) json_decode($json);
// access parsed json
echo $exchangeRates['rates']['GBP'];
but it did not work.
Now, when I try to access the "timestamp" value of the JSON file like this:
echo $exchangeRates['timestamp'];
it works fine.
Any ideas?
Try removing (array) in front of json_decode and adding true in second parameter
Here is the solution.
$exchangeRates = (array) json_decode($json,TRUE);
https://ideone.com/LFbvUF
What all you have to do is use the second parameter of json_decode function.
This is the complete code snippet.
<?php
$json='{
"success":true,
"timestamp":1430594775,
"rates":{
"AUD":1.273862,
"CAD":1.215036,
"CHF":0.932539,
"CNY":6.186694,
"EUR":0.893003,
"GBP":0.66046,
"HKD":7.751997,
"JPY":120.1098,
"SGD":1.329717
}
}';
$exchangeRates = (array) json_decode($json,TRUE);
echo $exchangeRates["rates"]["GBP"];
?>
Related
I'm doing a curl and getting the JSON result as:
$data = json_decode($result, true);
var_dump($data);
$data = $data["data"];
echo $data;
However, the echo of $data is "{". If I do a var_dump before assigning again the variable I see:
string(727796) "{"data":["base64_image1", "base64_image2",... ]}"
Why am I not able to access the data list? I just want to do a foreach over this list, but I cannot.
output is json so try to json_decode() again
I want to fetch data(the images in each post) stored in https://www.instagram.com/explore/tags/selfie/?__a=1, but all I get when I decode and var_dump this is NULL.
$obj = json_decode("https://www.instagram.com/explore/tags/selfie/?__a=1", true);
var_dump($obj);
Before decoding json you have to first fetch api response.
$obj = json_decode(file_get_contents("https://www.instagram.com/explore/tags/selfie/?__a=1"), true);
You're trying to json_decode the STRING
https://www.instagram.com/explore/tags/selfie/?__a=1
What you need to do is to fetch the URL first. I suggest using file_get_contents, which takes a URL and returns the contents at the end of that URL.
Try this:
$json = file_get_contents("https://www.instagram.com/explore/tags/selfie/?__a=1");
$obj = json_decode($json, true);
var_dump($obj);
The argument to the function json_decode(), $html must be plaintext/string.
This should work.
$url = "https://www.instagram.com/explore/tags/selfie/?__a=1";
$html = file_get_contents($url);
$obj = json_decode($html,true);
var_dump($obj);
See this in action here
I'm trying to get a specific column from an array for each record returned.
The array is called fields and one of the arrays in the array is locations. I'm looking for a specific column in the array called name.
Here's what I have:
foreach ($new_results as $result):?>
$locations = array_map($result->locations->location,function($obj) { return $obj->location; });
echo implode(",",$locations);
endforeach;
I'm connecting to a web service to pull this data. The above is the code that the company gave me, but they haven't tested it as far as I know. It doesn't work for me.
Here's the call to the API
$results = $connection->call('groups/getAll', $params=array("suspended" => "no","fields" =>"locations"));
$new_results = $results->groups->group;
Here's an example from the API.
{
"id": "xxxx",
"fields": {
"locations": [
"North",
"Central"
]
}
}
Any thoughts on what I'm doing wrong? I'm still very new to PHP so I may be missing something very obvious.
How to get json using curl:
How to get JSON data from Rest API by PHP Curl?
Now with that in mind you can dive a little deeper into the object manipulation in terms of the response here: Accessing JSON object elements in PHP.
//""" code from the first link
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = json_decode($curl_response, true);
print_r($curl_jason);
//""" code from the first link
&
//""" code from the second link
/*Variable passed in from the ExtJS interface as JSON object*/
$json = $_POST["newUserInfo"];
//$json = '{"USER":{"ID":"","FULL_USER_NAME":"Some Guy","ENTERPRISE_USER_NAME":"guyso01","USER_EMAIL":"Some.Guy#Email.com","USER_PHONE":"123-456-7890"},"PERMISSIONS":{"ID":"","USER_ID":"","IS_ADMIN":"true"},"SETTINGS":{"ID":"","USERS_ID":"","BACKGROUND":"default"}}';
//Test to view the decoded output
//var_dump(json_decode($json));
//Decode the $json variable
$jsonDecoded = json_decode($json,true);
//Create arrays for each table from the $jsonDecoded object
$user_info = array($jsonDecoded['USER']);
$permissions_info = array($jsonDecoded['PERMISSIONS']);
$settings_info = array($jsonDecoded['SETTINGS']);
// """ code from the first link
I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.
Here's the current code I have:
<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>
I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.
change:
$data = json_decode($jsondata, true);
to
$data = json_decode($json, true);
Also, json_decode returns an array so use:
echo $data['pc']['peak24'];
to access the data.
You first call the variable $json but then use $jsondata in json_decode.
You are missing the foreach cycle to fetch the two dimensional array $data:
<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) {
echo $pc["peak24"]."<br>";
}
?>
Check the $json and $jsondata that have different name but should be the same.
json response as below:
{"Success":true,"ErrorCode":0,"UserInformation":{"UserID":"19"}......
how do I get the UserID value by using json_decode?
I tried the below code with no luck.
$Response = json_decode($Response[2]);
echo $Response[0][0][0];
This should work:
<?php
$json = '{"Success":true, "ErrorCode":"0","UserInformation":{"UserID":"19"}}';
$response = json_decode($json, true);
echo $userId = $response["UserInformation"]["UserID"];
?>
Please check the PHP documentation of json_decode at http://php.net/json_decode
You can use object as well if you skip the second parameter of the json_decode function.