i am using json decoding function in php to decode this
{"department_id":"3123a79d-9e33-543f-c9be-4cc7ff79982c",
"ug_id":"217a783c-7970-8d92-6c99-5225a3ec533a",
"pg_id":"90da4eb5-6b75-44b0-2ce0-5226c8f60f8e",
"staff_id":"1e6364a3-0b3d-6384-a6c2-5225bd41c7fd",
"from_date":"date_start",
"to_date":"date_end"}
I have use json_decode($str) and json_decode($str,true) both, but i am getting null output. Please help me if possible.
Here is my complete code
$query='SELECT params FROM userreports WHERE id=\''.$_REQUEST['record'].'\'';
$query_exe=$db->query($query);
$res=$db->fetchByAssoc($query_exe);
$json=$res['params'];
$arr=json_decode($json,true);
echo '<pre>';
print_r($arr) ;exit;
Try following code it is working fine:
<?php
$str = '{"department_id":"3123a79d-9e33-543f-c9be-4cc7ff79982c",
"ug_id":"217a783c-7970-8d92-6c99-5225a3ec533a",
"pg_id":"90da4eb5-6b75-44b0-2ce0-5226c8f60f8e",
"staff_id":"1e6364a3-0b3d-6384-a6c2-5225bd41c7fd",
"from_date":"date_start",
"to_date":"date_end"}';
$arrStr = json_decode($str);
print_r($arrStr);
?>
Related
I am getting json response as mentioned below:
"{\"id\":\"order_DPUVoS2YVnBccy\",\"entity\":\"order\",\"amount\":100,\"amount_paid\":0,\"amount_due\":100,\"currency\":\"INR\",\"receipt\":\"7550\",\"offer_id\":null,\"status\":\"created\",\"attempts\":0,\"notes\":[],\"created_at\":1570082483}"
I want output to be as:
{"id":"order_DPUVoS2YVnBccy","entity":"order","amount":100,"amount_paid":0,"amount_due":100,"currency":"INR","receipt":"7550","offer_id":null,"status":"created","attempts":0,"notes":[],"created_at":1570082483}
I tried using stripslashes() to remove backslashes, but its not working.
What do you mean by "not working"?
<?php
$unescapedJson = '{\"id\":\"order_DPUVoS2YVnBccy\",\"entity\":\"order\",\"amount\":100,\"amount_paid\":0,\"amount_due\":100,\"currency\":\"INR\",\"receipt\":\"7550\",\"offer_id\":null,\"status\":\"created\",\"attempts\":0,\"notes\":[],\"created_at\":1570082483}';
echo stripslashes($unescapedJson);
Output for PHP 7.1.25 - 7.4.0rc2:
{"id":"order_DPUVoS2YVnBccy","entity":"order","amount":100,"amount_paid":0,"amount_due":100,"currency":"INR","receipt":"7550","offer_id":null,"status":"created","attempts":0,"notes":[],"created_at":1570082483}
you have to do it when json decode:
$data = json_encode('yourjsonvariable'), true, JSON_UNESCAPED_SLASHES);
There are many threads about this but none of them helped me solve this problem.
$array=array(
"dépendre"=>"to depend",
"dire"=>"to say",
"distraire"=>"distracted",
"être"=>"to be (being)",
);
Gets encoded like this with json_encode :
"d\u00e9pendre":"to depend","dire":"to say","distraire":"distracted","\u00eatre":"to be (being)"
So far I have tried this:
array_walk_recursive($array,function($value,$key) {
$key = urlencode(utf8_decode($key));
});
Try this,
json_encode($array, JSON_UNESCAPED_UNICODE);
You should get this result;
{
"dépendre":"to depend",
"dire":"to say",
"distraire":"distracted",
"être":"to be (being)"
}
Please check the result from the following code:
<?php
$x=array(
"dépendre"=>"to depend",
"dire"=>"to say",
"distraire"=>"distracted",
"être"=>"to be (being)",
);
$encoded = json_encode($x);
var_dump($x);
var_dump(json_decode($encoded, true));
The string you get in your question is a correctly escaped JSON and could be successfully decoded.
I'm trying to deserialize this json.
Actual I stay using the simple html dom library for get the web content, so the next step that I do is using the json_decode() function. But when I'll print the value returned by the function I get NULL. This is the code:
<?php
require_once("simplehtmldom_1_5/simple_html_dom.php");
$html = file_get_html('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable¶ms=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
What's wrong in my code? Maybe this isn't the best way for doing this? Hint me.
It seems that your file_get_html function is not working properly, you can get the content of a web with file_get_contents
<?php
$html = file_get_contents('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable¶ms=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
i have this public JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json and i need to extract data from it, right now i tried something like this:
$input = file_get_contents("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");
$json = json_decode($input);
echo $json[rates]->EUR;
but i obtain a blank page, any suggestion? Thanks !! Ste
json_decode either returns an object, or an array (when you use the second param). You are trying to use both.
Try:
$json = json_decode($input);
echo $json->rates->EUR;
OR
$json = json_decode($input, true);
echo $json['rates']['EUR'];
As to the blank page, please add the following to the top of your script:
error_reporting(E_ALL);
init_set('display_errors', true);
A 500 Error indicates you are unable to resolve that url using file_get_contents.
Check here for more information.
Read the help for json_decode ... without the second parameter it returns and object not an array ...
Either :
$json = json_decode($input);
echo $json->rates->EUR;
or
$json = json_decode($input,true);
echo $json['rates']['EUR'];
Try:
$arr = json_decode($input, true);
var_dump($arr);
echo $arr['rates']['EUR'];
Further Reading:
http://de.php.net/manual/de/function.json-encode.php
For anexample with cURL and SSL read this:
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
The link looks like starting with https, while file_get_contents can't deal with SSL. My suggestion is using curl.
How to transfer json data to html with php?
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey"
when I type the url in browser, it return
{"offset" : "0" , "results" : [{"body" : "A guide to cultural and recreational goings-on in and around the Hudson Valley. ...}]}
how to put the json body data into html? I mean like this echo '<div class="body"></div>';
You first need to get the file. You should use curl for this. In the example below I used the file_get_contents() function, you might want to replace it. Use json_decode() to parse the JSON for you.
$json = file_get_contents($url);
$data = json_decode($json);
echo $data->results[0]->body;
This will echo A guide to cultural....
Use json_decode() on the content of the file, which you can retrieve with file_get_contents($url), then you have an array you can use to build the HTML.
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$dataRaw = file_get_contents($url);
if ($dataRaw) {
$data = json_decode($dataRaw, true);
foreach ($data['results'] as $cEntry) {
?>
<div class="body">
<?php echo $cEntry['body']; ?>
</div>
<?php
}
}
I'm not sure why you would, but assuming fopen() URL opening is enabled, you could do...
echo file_get_contents($url);
like this ?
<?php
$url="http://api.nytimes.com/svc/search/v1/article?format=json&query=usa&rank=newest&api-key=mykey";
$json = file_get_contents($url);
echo $json;
Once you've loaded a JSON string into PHP, you can then convert it into a PHP array by using the function json_decode(). You can then print the appropriate array element into your HTML output as with any other PHP variable.
Try this:
$jsonDecoded = json_decode($yourJsonEncodedData);
echo $jsonDecoded->results->body;