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;
Related
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'];
}
I'm trying to get the json property's from an url. But just the first property arrives.
$json = file_get_contents('http://steamcommunity.com/market/priceoverview/?currency=3&appid=730&market_hash_name=' . $item_name);
$obj = json_decode($json, true);
$result = count($obj);
echo $json;
Output is:
"{"success":true}"
instead of:
"{"success":true,"lowest_price":"0,96\u20ac","volume":"143","median_price":"0,89\u20ac"}"
What am i doing wrong?
My json-data is as follows -
[{"name": "ram"}]
What I want is the value of name in a variable e.g., $fname. I have tried -
<?php
$jsondata = '[{"name": "ram"}]';
//$obj = json_decode($jsondata);
$obj = json_decode($jsondata,true);
print_r($obj); // This line outputs as :- Array ( [0] => Array ( [name] => ram ) )
//What I need is value of key
print_r($obj['name']);
foreach ($obj as $k=>$v){
echo $v;
}
?>
But I am unable to get desired output.
Here how to get that value
<?php
$jsondata = '[{"name": "ram"}]';
$obj = json_decode($jsondata,true);
//In case have multiple array
foreach ($obj as $k){
echo $k['name'];
}
//else
$obj[0]['name'];
//if
$jsondata = '{"name": "ram"}';
$obj = json_decode($jsondata,true);
//use
echo $obj['name'];
?>
As your output indicates, your JSON string represents an array containing one object. As you know that you want a value contained within the first element, you can get it directly:
$jsondata = '[{"name": "ram"}]';
$obj = json_decode($jsondata, true);
$name = $obj[0]['name'];
echo $name;
I try to put a JSON Object into a PHP Variable, it is not working. When I var_dump it, it says NULL.
PHP:
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$money = $data->tplc->stats; // -> What do i have to do to reach the id:GTA Online Cash and use the val: $33.9K ?
?>
JSON file example: (it changes over time)
{"cid":0,"ttl":"Grand Theft Auto V",
"ishs":false,"issa":false,"istc":false,"htl":true,"tlc":"text",
"tlt":"View Stats","tlh":"/member/drantifat/games/gtav/career/overview",
"iso":false,"cmng":false,"order":0,"uid":0,"ttc":0,
"tplc":"{\"game\":\"GTAV\",\"stats\":[{\"id\":\"Game Progress\",\"val\":\"58.77%\"},
{\"id\":\"Missions Passed\",\"val\":\"55\"},{\"id\":\"Playing Time\",\"val\":\"29:00:33\"},
{\"id\":\"GTA Online RP\",\"val\":\"2.4M\"},{\"id\":\"GTA Online Rank\",\"val\":\"128\"},
{\"id\":\"GTA Online Cash\",\"val\":\"$33.9K\"},
{\"id\":\"GTA Online Playing Time\",\"val\":\"529:44:12\"}],
\"url\":\"/member/drantifat/games/gtav/career/overview\"}","isa":false,"cnt":""}
Could anyone also help me with what I've mentioned in the PHP file?
tplc isn't a JSON object, it's a string that is itself JSON.
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$money = $tplc->stats;
?>
To get the money value specifically:
<?php
$json = utf8_encode(file_get_contents('http://socialclub.rockstargames.com/ajax/stat/1/profile_body/sta1/drantifat'));
$data = json_decode($json, false);
$tplc = json_decode($data->tplc);
$stats = $tplc->stats;
foreach ($tplc->stats as $stat) {
if ($stat->id == 'GTA Online Cash') {
$money = $stat->val;
break;
}
}
echo $money;
?>
My php receives a JSON string similar to this:
{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}
How do I get the value of "sub thing" ?
If I want "one-value":
$json = json_decode($data);
$service = $json->{'one-value'};
Try this:
$json = json_decode(
'{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}'
);
echo $json->{'sub-values'}->{'sub thing'};
// OR
$json = json_decode(
'{"one-value":"GOOGLE","sub-values":{"sub thing":"xpto"}}',
TRUE
);
echo $json['sub-values']['sub thing'];
json_decode($data,true);
will give you a complete indexed array of all valueson the JSON, after that you can do
$json["sub-values"]["sub thing"];
$json = json_decode($data,true);
$service = $json["sub-values"]['one-value'];
'true' is used to convert json string into associative array