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
Related
Link To JSON File: https://pastebin.com/gXcnNnUK
Code I've Tried So Far:
$json = json_decode($json, true);
foreach($json as $data){
echo $data['videoId'];
}
But it's not returning values. Is there any better approach to do it?
On line 3, did you mean to iterate over $arr instead of $json?
$json = file_get_contents('json.json');
$arr = json_decode($json, true);
foreach($arr['contents']['sectionListRenderer']['contents']['2']['itemSectionRenderer']['contents'] as $pero){
$video_id_list .= $pero['compactVideoRenderer']['videoId'].',';
}
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'];
}
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'm on PHP and I need to edit a JSON output to return only objects >=0 and divided by one hundred
Eg.
$json = {"data":[0,55,78,-32,-46,37]}
Needed
$json = {"data":[0,0.55,0.78,0.37]}
How this can be done?
Well, I know this is not the best practice, but if it's as simple as this, you can do the following.
$json = '{"data":[0,55,78,-32,-46,37]}';
// decoding the string to objects & arrays
$x = json_decode($json);
// applying a function on each value of the array
$x->data = array_map(
function($a)
{
if( $a >= 0 ) return $a/100;
else return null;
},
$x->data
);
// Removing empty values of the array
$x->data = array_filter($x->data);
// making a JSON array
$jsonData = json_encode(array_values($x->data));
// inserting a JSON array in a JSON Object
$json = '{"data":' . $jsonData . '}';
// here is your {"data":[0,0.55,0.78,0.37]}
echo $json;
Hope it helps !
Btw, I had to trick the json encode with array_values to prevent the creation of an object rather than an array for the data content. But I guess there is a better method that I just don't know ...
EDIT :
Find out the way :D
Once empty values removed from the array, just do :
$x->data = array_values($x->data);
$json = json_encode($x);
This will do the trick and it will not create issues with the rest of the object.
Alessandro:
Here's my approach, feel free to try. json_decode and a simple foreach can help you...
Code:
$json = array();
$result = array();
$json = '{"data":[0,55,78,-32,-46,37]}';
$decoded_json=json_decode($json, TRUE);
foreach ($decoded_json['data'] as &$value) {
if ($value >= 0){
$value = $value / 100;
$result[]=$value;
}
}
echo json_encode($result);
?>
Result:
[0,
0.55,
0.78,
0.37
]
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);