here is the json string in the variable which i am trying to parse with json_decode() function. I am trying to retrieve a specific node information with it but its showing me the blank white page. want to try with the file_get_contents() also to get json string from outer file. I have alreay seen the answer on the previous questions but that didn't helped me
<?php
//$json = file_get_contents('jsonfile.json');
$json = '[
{
"selfie": {
"post_author": "2",
"post_date": "2014-12-02 13:00:00",
"post_date_gmt": "2014-12-02 13:00:00",
"post_content": "this is an example content",
"image": "http://ssomesite.com/webservice/uploads/support.jpg",
"post_title": "TestJSON"
}
}
]';
$result = json_decode ($json);
echo $result->selfie->post_date;
//echo $result->selfie;
?>
echo $result[0]->selfie->post_date;
Your object path is wrong. It should be
echo $result[0]->selfie->post_date;
...because the JSON begins with an array, of which your selfie definition is in the first element, hence [0]
.
Your JSON format defines an array, containing a single object, with a single property called selfie
, you need to access the data like so:
echo $result[0]->selfie->post_date;
If ever a JSON string cannot be parsed, the best thing to do is check what json_last_error
and json_last_error_msg
tell you abot what went wrong.
I think you are missing that parent array. You need to consider the array also:
<?php
$json = '[
{
"selfie": {
"post_author": "2",
"post_date": "2014-12-02 13:00:00",
"post_date_gmt": "2014-12-02 13:00:00",
"post_content": "this is an example content",
"image": "http://ssomesite.com/webservice/uploads/support.jpg",
"post_title": "TestJSON"
}
}
]';
$result = json_decode ($json);
print($result[0]->selfie->post_date);
?>
That's an array object. Instead of using $result->selfie->post_date
. Use:
var_dump($result[0]->selfie->post_date);
<?php
//$json = file_get_contents('jsonfile.json');
$json = '[
{
"selfie": {
"post_author": "2",
"post_date": "2014-12-02 13:00:00",
"post_date_gmt": "2014-12-02 13:00:00",
"post_content": "this is an example content",
"image": "http://ssomesite.com/webservice/uploads/support.jpg",
"post_title": "TestJSON"
}
}
]';
$result = json_decode ($json,true);
$rs = (array)$result;
echo '<pre>';
print_r($rs);
die;
?>
Array
(
[0] => Array
(
[selfie] => Array
(
[post_author] => 2
[post_date] => 2014-12-02 13:00:00
[post_date_gmt] => 2014-12-02 13:00:00
[post_content] => this is an example content
[image] => http://ssomesite.com/webservice/uploads/support.jpg
[post_title] => TestJSON
)
)
)
After researching a lot i found that the file_get_contents() is actually getting data from the file but due to its encoding its having some junk character before json like this 
so for this i changed the encoding to utf8 (BOM or without BOM) and its works fine now.
with the json string in the same file was already answered by @rut2 and other developes too. I appreciate their effort to solve my problem.
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.