I have a string like this:
{
"update_id":659510742,
"message":{
"message_id":178,
"from":{
"id":110910409,
"first_name":"S.M_Emamian"
},
"chat":{
"id":-57184742,
"title":"Vvggg",
"type":"group"
},
"date":1446970836,
"new_chat_participant":{
"id":131677747,
"first_name":"Shadyab",
"username":"Shadyabbot"
}
}
}
now, I would like to get first_name. how can I get that ?
I tested below code, but it doesn't work :
$json_a = json_decode($content, true);
$first_name = $json_a->message->from->first_name; //it returns nothing.
$json_a = json_decode($content, true);
^^^^
That true forces the output of json_decode to be an array, but you are trying to treat it like an object. Remove that argument or use array syntax:
$first_name = $json_a['message']['from']['first_name'];
use only json_decode($content) remove true
try this code :-
$json_a = json_decode($content);
echo $json_a->message->from->first_name;
True convert the output of json_decode to be an array
$json_a = json_decode($content, true);
$first_name = $json_a->message->from->first_name; //it returns nothing.
That's because the function returns an associative array, and not an object.
By using -> you're trying to access object properties of $json_a. What you're really looking for is:
$json_a["message"]["from"]["first_name"]
This way you're accessing the associative array values by their key, and not trying to access object properties.
I hope this helped,
Sebastian
$json_a = json_decode($content);
$first_name = $json_a->message->from->first_name;
or
$json_a = json_decode($content,true);
$first_name = $json_a[message][from][first_name];
You're passing true as the second argument to json_decode which tells it to return an associative array rather than an object.
Either remove the second argument (or change it to the default, false), or access the fields using array syntax:
$first_name = $json['message']['from']['first_name'];
json_decode() is a php function used to decode json into either php standard object or an associative array dependent upon argument passed to it
When true it will return an associative otherwise by default it returns standard PHP object
According to your requirement please try executing following code snippet for retrieving first name from json document.
$json_a = json_decode($content, true);
$first_name = $json_a['message']['from']['first_name'];
For more detailed description regarding parsing json documents in php please refer the documentation in following URL
http://php.net/manual/en/function.json-decode.php
Related
$json = file_get_contents('module=API&method=Referrers.getReferrerType&format=json&period=day&date=yesterday&disableLink=1&idSite=3');
$data = json_decode($json,true);
json data:
[{"label":"Direct Entry","nb_uniq_visitors":526,"nb_visits":593,"nb_actions":768,"nb_users":0,"max_actions":32,"sum_visit_length":83153,"bounce_count":513,"nb_visits_converted":0,"segment":"referrerType==direct"},{"label":"Search Engines","nb_uniq_visitors":230,"nb_visits":235,"nb_actions":631,"nb_users":0,"max_actions":71,"sum_visit_length":52233,"bounce_count":150,"nb_visits_converted":0,"segment":"referrerType==search","idsubdatatable":2},{"label":"Websites","nb_uniq_visitors":7,"nb_visits":7,"nb_actions":20,"nb_users":0,"max_actions":5,"sum_visit_length":835,"bounce_count":1,"nb_visits_converted":0,"segment":"referrerType==website","idsubdatatable":3}]
I'm trying to display each label for the referrer type.
Direct Entry
Search Engines
Websites
$data[0]->label; doesn't work
$data->label; doesn't work
$data->label[0]; doesn't work
You passed true to the second parameter to json_decode(). That makes it return an associative array. Thus, you can simply use the data like this :
$data[0]["label"]
You can try this, as you're converting the json data to associative array (nested), by passing true into the 2nd argument of json_decode method:
$json = "[{\"label\":\"Direct Entry\",\"nb_uniq_visitors\":526,\"nb_visits\":593,\"nb_actions\":768,\"nb_users\":0,\"max_actions\":32,\"sum_visit_length\":83153,\"bounce_count\":513,\"nb_visits_converted\":0,\"segment\":\"referrerType==direct\"},{\"label\":\"Search Engines\",\"nb_uniq_visitors\":230,\"nb_visits\":235,\"nb_actions\":631,\"nb_users\":0,\"max_actions\":71,\"sum_visit_length\":52233,\"bounce_count\":150,\"nb_visits_converted\":0,\"segment\":\"referrerType==search\",\"idsubdatatable\":2},{\"label\":\"Websites\",\"nb_uniq_visitors\":7,\"nb_visits\":7,\"nb_actions\":20,\"nb_users\":0,\"max_actions\":5,\"sum_visit_length\":835,\"bounce_count\":1,\"nb_visits_converted\":0,\"segment\":\"referrerType==website\",\"idsubdatatable\":3}]";
$data = json_decode($json, true);
print_f($data[0]['label']);
Hope this helps!
{
"firstName":"sunny",
"religion": {"holly":"1",
"bolly":"colly",
"nolly":"only"
},
"lonely":"money",
"ronney":"leone",
"honey":"bunny"
}
This is my JSON. I want to get all the data from this and to be stored into some php variables or an array.
I Used the following code to extract data from my JSON. I decoded it first and then stored it in an array..
$val_array = json_decode($jsondata, true);
echo $jsondata;
$AAA = $val_array->firstName;
$BBB = $val_array->religion;
$CCC_id = $val_array->bolly;
$DDD = $val_array->nolly;
$CCC_id = $val_array->lonely;
$DDD = $val_array->ronney;
But it didn't give me any output. Then I used this.
foreach($data['val_array'] as $item)
{
echo $item[0];
}
}
No output. Help??
You get this second param wrong:
$val_array = json_decode($jsondata, true);
$AAA = $val_array['firstName'];
OR
$val_array = json_decode($jsondata, false);
$AAA=$val_array->firstName;
Your JSON is not valid. Remove commas after last elements:
{
"firstName" : "sunny",
"religion" : {
"holly" : "1",
"bolly" : "colly",
"nolly" : "only" # Here
},
"lonely" : "money",
"ronney" : "leone",
"honey" : "bunny" # And here
}
You have an error in your JSON :
"nolly":"only",
"honey":"bunny",
remove the ',' at the end of these 2 lines, then json_decode() will return you an array.
And if you want an object, do not pass second argument to json_decode()
json_decode by default returns an object yet since you are setting the second parameter to true, you are given an associative array with the information instead.
It basically comes down to the fact that either you do not need to fill in the second parameter and get the object you want, or you work with arrays when you set the parameter to true.
A little reading on PHP.net will do you good for further reference since their documentation is well presented, usually commented by others with helpful suggestions and quite clean as well!
$val_array = json_decode($jsondata, true);
$m1=$val_array['firstName'];
$m2=$val_array['lonely'];
$m3=$val_array['ronney'];
$m4=$val_array['honey'];
$m4=$val_array['religion']['holly'];
$m5=$val_array['religion']['bolly'];
$m6=$val_array['religion']['nolly'];
BY using this, we don't have to use foreach loops or inner loops for accessing data. Viola!
I'm trying to decode the following JSON using php json_decode function.
[{"total_count":17}]
I think the square brackets in output is preventing it. How do I get around that? I can't control the output because it is coming from Facebook FQL query:
https://api.facebook.com/method/fql.query?format=json&query=SELECT%20total_count%20FROM%20link_stat%20WHERE%20url=%22http://www.apple.com%22
PHP's json_decode returns an instance of stdClass by default.
For you, it's probably easier to deal with array. You can force PHP to return arrays, as a second parameter to json_decode:
$var = json_decode('[{"total_count":17}]', true);
After that, you can access the variable as $result[0]['total_count']
See this JS fiddle for an example of how to read it:
http://jsfiddle.net/8V4qP/1
It's basically the same code for PHP, except you need to pass true as your second argument to json_decode to tell php you want to use it as associative arrays instead of actual objects:
<?php
$result = json_decode('[{"total_count":17}]', true);
print $result[0]['total_count'];
?>
if you don't pass true, you would have to access it like this: $result[0]->total_count because it is an array containing an object, not an array containing an array.
$json = "[{\"total_count\":17}]";
$arr = Jason_decode($json);
foreach ($arr as $obj) {
echo $obj->total_count . "<br>";
}
Or use json_decode($json, true) if you want associative arrays instead of objects.
I am new to PHP. I am using json_encode to convert an array into json data, and decode it using json_decode in another file. However, I am getting json error as syntax error.
My code is as follows:
File 1:
$result = get_data_array();
exit(json_encode($result));
File 2:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
$data->name // name is the array key
However, I am getting an error as:
Trying to get property of non-object.
You passed true to the second parameter of json_decode so it will return an array.
Use this:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
echo $data['name'];
I am trying to read certain values from a json string in php, I am able to do a simple json string with only one value such as
$json = '{"value":"somevalue"}';
Using this:
<?php
$json = '{"value":"somevalue"}';
$obj = json_decode(json_encode($json));
print $obj->{'value'};
?>
But when i try an get a value from the following json string it throws an error...
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
I validated the json on JSONlint but not sure how to access the values within this with php.
Thanks
You can try this:
$json = '{"field": "title","rule": {"required": "true","minlength": "4","maxlength": "150" }}';
//since $json is a valid json format you needn't encode and decode it again
$obj = json_decode($json);
print_r($obj->filed);
print_r($obj->rule);
You can pass true as a second parameter to json_decode() to get the results as an array
$my_arr = json_decode($json, true);
var_dump($my_arr);
Should help you. You can then step through the array as you would normally.
use var_dump to print out the object with all it's members and hierarchy. you should then be able to find the value you are looking for