How can I get the source, title, issn, author, ... from a json file: JSON file
We tried with:
$new_pmid = $_POST['new_pmid'];
$api_json_url = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?db=pubmed&id=".$new_pmid."&retmode=json";
$json = file_get_contents($api_json_url);
$data = json_decode($json, TRUE);
echo $header[0]->result->$new_pmid->title;
....
But nothing happen...
Can you give me the solution for the json file (generated from pubmed database).
Thank you.
You didn't use the $data variable, which stored the decoded data
You decode JSON into $data as array
$title = $data['result'][$new_pmid]['title'];
$issn = $data['result'][$new_pmid]['issn'];
$authors = $data['result'][$new_pmid]['authors'];
--Update--
To get $authors name ,authtype ,... use foreach loop:
foreach($authors as $author){
$name = $author['name'];
$authtype = $author['authtype'];
$clusterid = $author['clusterid'];
}
Related
I want to get the imdb_id from inside json using foreach
i am having an issue with getting it decoded and displayed.
My php code so far:
$themoviedburl = "http://api.themoviedb.org/3/tv/60625/external_ids?api_key=";
$get = file_get_contents($themoviedburl);
$decode1 = json_decode($get, TRUE);
foreach($decode1['external_ids'] as $value){
$imdb = $value['imdb_id'];
}
Json:
{"id":60625,"imdb_id":"tt2861424","freebase_mid":"/m/0z6p24j","freebase_id":null,"tvdb_id":275274,"tvrage_id":33381,"facebook_id":"RickandMorty","instagram_id":"rickandmorty","twitter_id":"RickandMorty"}
After $decode1 = json_decode($get, TRUE);
You can simply do $imdb_ids = array_column($decode1['external_ids'],'imdb_id'); to collect all imdb_id in one array.
More info on array_column : https://www.php.net/manual/en/function.array-column.php
As discussed in the comments, to get only one imdb_id ID, you can do like below:
$json = '{"id":60625,"imdb_id":"tt2861424","freebase_mid":"/m/0z6p24j","freebase_id":null,"tvdb_id":275274,"tvrage_id":33381,"facebook_id":"RickandMorty","instagram_id":"rickandmorty","twitter_id":"RickandMorty"}';
$decoded_data = json_decode($json,true);
$imdb_id = $decoded_data['imdb_id'];
The API is:
{"status":1,"data":[{"address":"0x5c48aebbbbdcf44f5f181edbb5b20a68210eacfe","balance":6000000000000000000,"nonce":null,"code":"0x","name":null,"storage":null,"firstSeen":null}]}
I am trying to get the value from Balance (6000000000000000000). I also need to divide this value with 1000000000000000000 in meantime...
I tried:
$obj = json_decode($json,true);
$address = $obj['address'];
$json = file_get_html('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
$v = (get_object_vars($obj->data));
$balance = $v['balance'];
$_SESSION['balance'] = $balance;
It doesn't work.
The same code works with the API below:
{"status":"success","data":{"address":"LU4P8fVNj8xv2dwRc7fdXmpaW2xuCSJvUK","is_unknown":false,"balance":0,"balance_multisig":0,"totalreceived":0.0340576,"nb_txs":11,"first_tx":{"time_utc":"2015-08-30T09:51:05Z","tx":"10f4b55d0bead8d3d84fe27433db20b63368d65bc043c651d59dbe1342d14098","block_nb":"842330","value":0.00704229,"confirmations":251241},"last_tx":{"time_utc":"2016-02-02T16:09:23Z","tx":"c666d3acf6f57fd86a2ccc9537ee022167da408f26193e4abbd7b8148fc518b3","block_nb":"932771","value":-0.01250452,"confirmations":160800},"is_valid":true},"code":200,"message":""}
What am I doing wrong?
Its because you're missing that the address is located in the first data array in the JSON string and in the second JSON string, no array exists inside the data array.
$obj = json_decode($json,true);
$address = $obj['data'][0]['address'];
$json = file_get_contents('https://etherchain.org/api/account/'.$address);
$obj = json_decode($json);
echo $obj->data[0]->balance;
I have an API which is returning some nested JSON data with multiple levels. My PHP code to loop through is below but I'm not getting any output:
$data = json_decode($output, true);
foreach($data as $item){
$title = $item->events->name->text;
echo $title;
}
An example of the data can be found here: http://i.imgur.com/Y55vl7n.png
I am trying to print the text name of each of the events (events->name->text)
There is a problem in your code, when you decode the json string, you use:
$data = json_decode($output, true);
It is converting everything to "array" (http://php.net/manual/en/function.json-decode.php), so you cannot access it like if they were objects.
You have to do:
foreach($data as $item){
$title = $item["events"]["name"]["text"];
echo $title;
}
Hope this helps!
I have the following problem. When i am trying to read some json data that are posted from an html page, i'm facing with the following error "Trying to get property of non-object on line".
Jquery script to create the json
var json = {"data":[]};
json.data.push({serialNumber: $serialNumber, xreosi: $xreosiToPost,
forma: $forma, apolia: $apolia});
Jquery for posting to php
$.post("page.php",{jsonData: JSON.stringify(json),
customer: $("#cusID").val()},function(data){});
PHP file
$json = json_decode($_POST['jsonData']);
foreach($json as $value){
$serialNumber = $value->serialNumber;
echo $serialNumber;
}
Thanks in advance.
Thereafter:
var json = {"data":[]};
json.data.push({serialNumber: $serialNumber, xreosi: $xreosiToPost,
forma: $forma, apolia: $apolia});
You have:
Object[data][0] = array('serialNumber' => ...);
Need:
$json = json_decode($_POST['jsonData'][0]);
or
$json = json_decode($_POST['jsonData']);
foreach($json as $row){
foreach($row as $value) {
$serialNumber = $value->serialNumber;
echo $serialNumber;
}
}
json_decode without second parameter returns result as php object. You have to pass true as second parameter. Also your data are in $json['data'], not $json:
$json = json_decode($_POST['jsonData'], true);
foreach($json['data'] as $value) {
$serialNumber = $value->serialNumber;
echo $serialNumber;
}
i have this JSON
{
"count":"3",
"num":"1",
"array":[
{
"id":"a_a",
"amount":56,
"duration":"0:12",
"time":1234566,
"type":0
},
{
"id":"a_a",
"amount":56,
"duration":"0:12",
"time":1234566,
"type":1
}
]
}
created it in android and send it by **HttpPost**
, i've tried a lot of ways to get the data in the php, my php file is this:
<?php
$response = array();
//$json_data = json_encode(stripslashes($_POST['jsonarray']))
$josn = file_get_contents("php://input");
$json_data = json_decode($josn,true);
$count = $json_data->{'count'};
$num = $json_data["num"];
$response["data"]=$count;
$response["data2"]=$num;
// echoing JSON response
echo json_encode($response);
?>
but $count and $num always returns null, any help please, and thanks.
$json_data = json_decode($josn,true);
this returns an array, not an object (which you are using later in the code). Use this to convert the json string to an object:
$json_data = json_decode($josn);
Or you could just use arrays, in which case your code should look like:
<?php
$response = array();
//$json_data = json_encode(stripslashes($_POST['jsonarray']))
$josn = file_get_contents("php://input");
$json_data = json_decode($josn, true); // using true to get array
$count = $json_data["count"]; // accessing array values as normal
$num = $json_data["num"]; // accessing array values as normal
$response["data"] = $count; // instead of setting $count first you could just add
$response["data2"] = $num; // the json_data array values directly to the response array
// echoing JSON response
echo json_encode($response);