Can anyone please shows me how to get the input of PHP to JSON file (.json) and read data from JSON file and display in PHP (Echo).
for example:
$myObj->name = "John";
$myObj->age= 20;
to result.json
{"name":"John","Age":20}
and retrieve from result.json and display data in PHP as
name=John
Age=20
To convert the object to json use this:
$json = json_encode($myObj);
See the json_encode docs.
To return it back to the format you want try this...
$obj = json_decode($json);
$name = $obj->name; // John
$age = $obj->age; // 20
See the json_decode docs.
To iterate over keys and values do something like this:
foreach($obj as $key=>$value)
{
echo $key . " = " . $value . "\n";
}
json_encode() is used for encoding PHP data into a JSON format and json_decode() is used to decode JSON into a PHP data
json_encode documentation:
http://php.net/manual/en/function.json-encode.php
json_decode documentation
http://php.net/manual/en/function.json-decode.php
if you are having an php array you can convert it into json.
$json_string = json_encode($array);
and write this into a json file.
$fp = fopen('results.json', 'w');
fwrite($fp, json_string);
fclose($fp);
now convert your json string which is in results.json to array.
$str = file_get_contents('./results.json');
$array = json_decode($str, true); // decode the JSON into an associative array
Related
I have a .txt file called 'test.txt' that is a JSON array like this:
[{"email":"chrono#gmail.com","createdate":"2016-03-23","source":"email"}]
I'm trying to use PHP to decode this JSON array so I can send my information over to my e-mail database for capture. I've created a PHP file with this code:
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
echo $json;
?>
For some reason, it's not echoing the decoded JSON when I visit my php page. Is there a reason for this and can anyone shed some light? Thanks!
You use echo to print scalar variables like
$x = 'Fred';
echo $x;
To print an array or object you use print_r() or var_dump()
$array = [1,2,3,4];
print_r($array);
As json_decode() takes a JSON string and converts it to a PHP array or object use print_r() for example.
Also if the json_decode() fails for any reason there is a function provided to print the error message.
<?php
$url = 'http://www.test.com/sweeps/test.txt';
$content = file_get_contents($url);
$json = json_decode($content,true);
if ( json_last_error() !== JSON_ERROR_NONE ) {
echo json_last_error_msg();
exit;
}
You'll need to split that json string into two separate json strings (judging by the pastebin you've provided). Look for "][", break there, and try with any of the parts you end up with:
$tmp = explode('][', $json_string);
if (!count($tmp)) {
$json = json_decode($json_string);
var_dump($json);
} else {
foreach ($tmp as $json_part) {
$json = json_decode('['.rtrim(ltrim($json_string, '['), ']').']');
var_dump($json);
}
}
Hi Guys I have a JSON data I need to convert this data Treeview a json data url: http://torrent2dl.ml/json.php
recovered state = http://torrent2dl.ml/json.php?tree
I tried to do http://torrent2dl.ml/hedef.php
how to convert this data a php function or code ?
json_decode($jsonObject, true);
Use json_decode() :
<?php
$url = 'http://torrent2dl.ml/json.php';
$JSON = file_get_contents($url);
// echo the JSON (you can echo this to JavaScript to use it there)
echo $JSON;
// You can decode it to process it in PHP
$data = json_decode($JSON);
var_dump($data);
?>
Source : https://stackoverflow.com/a/8344667/4652564
I need to decode this json value using PHP. I need the value 'url'.
Here is what I have but does not work.
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded[???]; // NEED THIS VAR.
Since $decoded is a PHP array you can access it like any other array:
$url = $decoded['data']['url'];
$decoded=(object)json_decode( $json['data'], true );
echo $decoded->url
This worked:
$json = file_get_contents('http://graph.facebook.com/{fb-ID}/picture?type=large&redirect=false');
$decoded = json_decode($json,true);
$url = $decoded['data']['url'];
How do I use json variable in php.
$filePath = '/home/user/public_html/uploads/samplefile.txt'
echo '{"status":"success", "fileName" : "'.$filePath.'"}';
Say I would like to use it this way.
$mail->addattachment($fileName);
Thanks.
You need to first deserialize the json data into a PHP array.
$json_string = "...."; //this is your json string
$json_data = json_decode($json_string); // decode into an array
$mail->addattachment($json_data['filename']);
While #xbonez answer is correct, you could use it the way you specified, but it's not recommended:
$json = <<<JSON
{"status":"success", "fileName" : "/home/user/public_html/uploads/samplefile.txt"}
JSON;
extract(json_decode($json, TRUE));
echo $fileName;
Im try export json result to csv and save data as file, im try with something like this
$getFile = file_get_contents('JSON_URL');
$json_obj = json_decode($getFile);
$fp = fopen('/home/xxxx/public_html/xxxx/api/export/tmp/file.csv', 'w');
foreach ($json_obj as $row) {
fputcsv($fp, $row);
}
fclose($fp);
but seems not working
Here's an example json format for link above
[
{key:value,key:value...}
...]
in order for your code to work as expected, try decoding the json object as an associative array. This is done by passing a boolean true to the 2nd param of json_decode
$json_obj = json_decode($getFile, true);