I tried retrieving the word "John" for example using this way.
Code
$decoded = json_decode('$thejson');
$myvar = $decoded->name;
echo $myvar;
JSON
[
{
"name": "John",
"age": "50",
"Job": "Developer"
}
]
I think the first squarre brackets are the thing blocking me here
Thanks for the help
It should work this way:
$myvar = $decoded[0]->name;
Try this:
$decoded = json_decode($thejson,TRUE);
$myvar = $decoded[0]['name'];
Related
I want this json output into php array:
{
"data": [
{
"id": "1",
"name": "Roger",
"country": "Switzerland",
"city": "Basel"
},
{
"id": "2",
"name": "Rafael",
"country": "Spain",
"city": "Madrid"
},
]
}
I am trying this:
$arrResult = array();
$arrResult['data'] = array();`
while($row = mysqli_fetch_assoc($result)){`
$id = $row['id'];
$name = $row['name'];
$country = $row['country'];
$arrResult['data'][] = array(
'id'=> $id,
'name'=> $name,
'country'=> $country,
);
}
echo json_encode($arrResult, JSON_FORCE_OBJECT);
I want the same output as given into JSON Format from a php array.
I have found your problem. Remove the comma in the json (explained in the code).
<?php
$json = '{
"data": [
{
"id": "1",
"name": "Roger",
"country": "Switzerland",
"city": "Basel"
},
{
"id": "2",
"name": "Rafael",
"country": "Spain",
"city": "Madrid"
}, <--- THIS IS A PROBLEM
]
}';
/* in order for this function to work you need to delete that comma.
Because there isn't another list in json so comma should not be there */
$arrResult = json_decode($json, true);
print_r($arrResult);
You can use json_decode() if you are using PHP >= 5.2.0
Here is a sample on how to use json_decode()
<?php
// JSON string
$someJSON = '[{"name":"Shriram","gender":"male"},{"name":"Lexa","gender":"female"},{"name":"John Doe","gender":"male"}]';
// Convert JSON string to Array
$someArray = json_decode($someJSON, true);
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["name"]; // Access Array data
// Convert JSON string to Object
$someObject = json_decode($someJSON);
print_r($someObject); // Dump all data of the Object
echo $someObject[0]->name; // Access Object data
?>
you can use the json_decode function.
example;
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
echo $json;
$array = json_decode($json, true);
print_r($array);
?>
What I've understood is that you want to encode your array into the json format shown.
Use json_encode($arrResult) instead of json_encode($arrResult, JSON_FORCE_OBJECT).
JSON_FORCE_OBJECT outputs an object rather than an array.
I've JSON as follows:
[
{
"id": "..",
"size": "..",
"task": ".."
},
{
"id": "..",
"size": "..",
"task": "..",
},
...
]
My task is to get plain JSON code for each element (array). So, for every id (seperate element) I would get it's json information (size, task ect...). How can this be done?
Update: Is there any better way than following?
json_decode and then loop over each array (because this would give me decoded version, which I don't want right now, so after this procedure, I'd have to encode result again).
Use json_decode()
<?php
$json = array(array('fname'=> "aman", 'lname' => 'kumar', 'email'=> 'developer.amankr#gmail.com'));
$data = json_encode($json); //[{"fname":"aman","lname":"kumar","email":"developer.amankr#gmail.com"}]
$parse = json_decode($data);
echo $fname = $parse[0]->fname;
echo $lname = $parse[0]->lname;
echo "<br>";
echo $email = $parse[0]->email;
?>
I have a PHP page where I reference a JSON object that looks like this:
{
"body": {
"zip": "02110",
"stores": [
{
"storeEmail": "email#email.com",
"storeName": "Name",
"city": "City",
"Availability": {
"123": {
"Quote": "daily",
"Display": "available",
}
},
},
Each JSON object contains multiple "stores", above is one example.
I can currently echo the store name by using this:
echo "<br>".$phpArray->body->stores{0}->storeName;
How do I echo the value "123" from the sample JSON? I would also like to echo the quote as a separate variable. The value "123" will change for different searches. Any help would be greatly appreciated!
$phpArray = json_decode($json, true);
foreach($phpArray['body']['stores'] as $store) {
echo $store['storeName'];
foreach{$store['Availabilty'] as $avail => $info) {
echo $avail; // 123
echo $info['Quote'];
}
}
$b = json_decode($a);
var_dump(key($b->body->stores{0}->Availability));
var_dump(reset($b->body->stores{0}->Availability)->Quote);
or a loop for stores
foreach($b->body->stores as $store) {
var_dump(key($store->Availability));
var_dump(reset($store->Availability)->Quote);
}
i've the following json data with the following structure
{
"page": 0,
"items": 43,
"total": 43,
"incentiveItems": {
"incentiveItem": [
{
"#id": "1111",
"name": "...",
"program": {
"#id": "6765",
"$": "NAME"
},
"admedia": {
"admediumItem": {
...
and more, to parse the data in php i do a simple foreach...
$obj = json_decode($jsonData);
echo $obj->items;
echo '<table><tr><td>Nome</td><td>Descrizione</td><td>Codice</td></tr>';
foreach($obj->incentiveItems->incentiveItem as $programma ) {
so, the problem is that i can't call the "$" :" NAME" value, coz if i use (for ex)
$programma->program->$
$programma->program['$']
$obj->incentiveItems->incentiveItem->program->$ or ['$']
it crash
and the same is for the #id field.
can someone help me? Thanks
Tyr decoding json as associative array:
$obj = json_decode($jsonData, true);
echo $obj['items'];
echo '<table><tr><td>Nome</td><td>Descrizione</td><td>Codice</td></tr>';
foreach($obj['incentiveItems']['incentiveItem'] as $programma ) {
echo $programma['#id'];
I believe you can use single quotes and curly brackets to access it
$obj->incentiveItems->incentiveItem->program->{'$'}
$obj->incentiveItems->incentiveItem->program->{'#id'}
I would like to know how to count how many people follows someone in Instagram and place the number in a var, Instagram gives you this link:
https://api.instagram.com/v1/users/3/followed-by?access_token=xxxxxxxxx.xxxxxxxxxxxxxxxxxxxx
And displays a result like so
{
"data": [{
"username": "meeker",
"first_name": "Tom",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_6623_75sq.jpg",
"id": "6623",
"last_name": "Meeker"
},
{
"username": "Mark",
"first_name": "Mark",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
"id": "29648",
"last_name": "Shin"
},
{
"username": "nancy",
"first_name": "Nancy",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
"id": "13096",
"last_name": "Smith"
}]
}
How can I count how many are there and place it in a var, lets say:
<? echo "You are been follow by ".$followers." users!"; ?>
To display: You are been follow by 3 users!
You would need to use json_decode to to decode the JSON response, then access the resulting object's data attribute (an array of 'follower' objects), and count that:
$json = '{
"data": [{
"username": "meeker",
"first_name": "Tom",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_6623_75sq.jpg",
"id": "6623",
"last_name": "Meeker"
},
{
"username": "Mark",
"first_name": "Mark",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
"id": "29648",
"last_name": "Shin"
},
{
"username": "nancy",
"first_name": "Nancy",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
"id": "13096",
"last_name": "Smith"
}]
}';
$json = json_decode($json);
echo "You have " .count($json->data) ." followers"
OR
$json = json_decode($json,true);
echo "You have " .count($json['data']) ." followers"
You are getting as a json string, you need to decode it using json_decode.
$data = json_decode($string,true);
$followers = count($data['data']);
CodePad Demo.
You need to use json_decode() which will return you a PHP array. Then all you need to do is to count() all the values in the array with 'data' key.
You can use json_decode
$array = json_decode($str);
Then give
echo count($array);
It will give total count of users
simple way to count entries returned asJSON
echo count(json_decode($followers);
Use json_decode() to create a PHP array from the JSON. Then you can simply do a count() on that:
$jsonData = json_decode($yourAPIResult);
echo count($jsonData->data);
Be aware, however, that you probably should set up some error handling in case the API did not return a proper JSON string. So something like this might be better:
if (is_null($jsonData) || !property_exists($jsonData, 'data')) {
echo '?';
} else {
echo count($jsonData->data);
}