I'm retrieving data from Wikipedia API in JSON format:
{"batchcomplete":"","continue":{"gsroffset":2,"continue":"gsroffset||"},"query":{"pages":{"42068":{"pageid":42068,"ns":0,"title":"Freddie Mercury","index":2},"5119376":{"pageid":5119376,"ns":0,"title":"John F. Kennedy","index":1}}}}
How can I get title data?
I've tried strstr, but it works only for 1 result (as you can see, there are two of them).
I've tried json_decode into array $json = json_decode($url, true);, but the array is multidimensional and I'm struggling to get the data out of it.
What can I do?
Well, you should use json_decode and not use string methods.
This is how you can get access to the title of each page:
$data = json_decode($jsonData, true);
$pages = $data['query']['pages'] ?? [];
foreach ($pages as $page) {
echo $page['title'];
}
Related
i have searched and searched but i don't know where i'm wrong getting a value from JSON encode, can you help me? Please don't kill me, i'm a newbie :)
My php:
<?php
$data = json_decode("document.json", true);
$getit = $data["likes"];
My JSON:
[{
"title" : "MYTITLE",
"image" : "MYIMAGE",
"likes" : 0
}]
EDIT
Thanks for the help this is now working!
json_decode expects an string, not an filename - so you have first get the contents of the given file. This could be easily achieved with file_get_contents.
Your current json structure contains an array(with currently only one element), which contains an object. So if you want the likes, you have to read the first element of the result array and of that(an associative array), the likes.
$data = json_decode(file_get_contents($filename), true);
$likes = $data[0]['likes'];
If you have more than one object in the given json file, you could loop over the data with foreach
foreach ($data as $element) {
echo "likes of {$element['title']}: {$element['likes']}\n";
}
For json_decode you have to pass JSON string, not a file name. Use file_get_contents to get JSON content and then decode it.
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]['likes'];
Your JSON is an array of objects, you first need to access the first element of your array :
$data[0]
Then you can access the key you want :
$getit = $data[0]['likes'];
Plus, as stated in a comment above, you need to pass a string to json encode, here is an code sample that should suit your needs :
<?php
$data = json_decode(file_get_contents('document.json'), true);
$getit = $data[0]["likes"];
Hope this helps.
I want to decode json file resulted from DuckDuckGo API into a readeble html or PHP string.
I try with PHP json_decode, but nothing:
$object = json_decode($string, true);
echo $object['RelatedTopics']['Result'];
Any ideas?
From the JSON responde you posted, it is possible to see that RelatedTopics is an array. So you must first access an element of this array to then access the Result key:
echo $object['RelatedTopics'][0]['Result'];
Or in a loop just to test:
foreach ($object['RelatedTopics'] as $rel)
echo $rel['Result'];
I've have this list of products in JSON that needs to be decoded:
"[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]"
After I decode it in PHP with json_decode(), I have no idea what kind of structure the output is. I assumed that it would be an array, but after I ask for count() it says its "0". How can I loop through this data so that I get the attributes of each product on the list.
Thanks!
To convert json to an array use
json_decode($json, true);
You can use json_decode() It will convert your json into array.
e.g,
$json_array = json_decode($your_json_data); // convert to object array
$json_array = json_decode($your_json_data, true); // convert to array
Then you can loop array variable like,
foreach($json_array as $json){
echo $json['key']; // you can access your key value like this if result is array
echo $json->key; // you can access your key value like this if result is object
}
Try like following codes:
$json_string = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$array = json_decode($json_string);
foreach ($array as $value)
{
echo $value->productId; // epIJp9
echo $value->name; // Product A
}
Get Count
echo count($array); // 2
Did you check the manual ?
http://www.php.net/manual/en/function.json-decode.php
Or just find some duplicates ?
How to convert JSON string to array
Use GOOGLE.
json_decode($json, true);
Second parameter. If it is true, it will return array.
You can try the code at php fiddle online, works for me
$list = '[{"productId":"epIJp9","name":"Product A","amount":"5","identifier":"242"},{"productId":"a93fHL","name":"Product B","amount":"2","identifier":"985"}]';
$decoded_list = json_decode($list);
echo count($decoded_list);
print_r($decoded_list);
Hi I'm trying to loop through a json array of objects but I literally get nothing back, here is my attempt.
$jsonurl = 'http://eol.org/api/search/1.0.json?q='."$searchvar".'&page=1&exact=false& filter_by_taxon_concept_id=&filter_by_hierarchy_entry_id=&filter_by_string=&cache_ttl=';
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_decode($json);
foreach ($json_output->dataObjects as $objects){
print "{$objects->title}\n";
}
here is the structure of the actual json array.
array
0 object
id 19076
title Vulpes
link http://eol.org/19076?action=overview&controller=taxa
content Arctic foxes; kit foxes; red foxes; red fox
Try:
foreach ($json_output->results as $object){
print "{$object->title}\n";
}
one more approach that you can try is to decode the json in to array instead of objects.
$json_output = json_decode($json,true);
By passing a varaible as true in the json_decode() function you will get the json_output as an array which can be easily traversed.
Else
you can loop through
foreach ($json_output->results as $object){
echo "{$object->title}\n";
}
Please refer to the json_decode() function php manual guide at http://php.net/manual/en/function.json-decode.php
i hope this could be helpful to you
try to type cast with object
foreach ($json_output->dataObjects as $objects){
let me know if i can help you more
I'm working on a side project and at its core, I need to get a foursquare json feed into an array that i can loop through. My code is below and results in the following error:
Warning: Invalid argument supplied for foreach() in /homepages/7/d346835943/htdocs/dealrub/results.php on line 56
Here is an example of the json feed that i am correctly acquiring:
$jsonurl = "http://api.foursquare.com/v2/venues/search?ll=".$lat.",".$lon."&limit=100";
$json = file_get_contents($jsonurl,0,null,null);
$json_output = json_encode($json, true);
foreach ( $json_output->response->groups[0]->items as $items )
{
echo "{$items->name}\n";
}
Any help as to what i'm doing wrong would be greatly appreciated. I left the jsonurl without my api key, but it is successfully returning the json results.
You have to use json_decode.
Check whether $json_ouput is not empty.
You are passing true as second argument to json_decode (assuming you have it right) which means that it returns an associative array.
Either omit that:
$json_output = json_decode($json);
or access items as array:
foreach ( $json_output['response']['groups'][0]['items'] as $items )
You're using json_encode on a string that is already in json. Try json_decode instead ;)