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
Related
I printed this json from a get_file_contents output.
{"status":"INVALID_CREDENTIALS"}
Now I want to echo the content of "status" and I used
echo $status
but not working. Please help.
First, use json_decode to convert to php object:
$json = '{"status":"INVALID_CREDENTIALS"}';
$obj = json_decode($json);
Then:
echo $obj->status;
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
I have a PHP file which is displaying some posted data:
$data = file_get_contents('php://input');
echo json_encode($data);
The above returns:
{"name":"mark","item":"car"}
Now I want to echo just the name so I tried:
echo $data[0].name;
But that's giving me Error: [Object].[Object]
How can I fix this?
You need to decode your JSON input first:
// $data is an input string
$data = file_get_contents('php://input');
// convert input string to PHP array
$data = json_decode(data, true);
// echo just the name
echo $data['name'];
// dump the whole parsed input
var_dump($data);
I have sent the serialized data to PHP on form submission:
$('#new-store-pickup').on("click",function(){
var businessHoursManager = $("#businessHoursContainer3").businessHours();
$('#businesshourvalue').val(JSON.stringify(businessHoursManager.serialize()));
$('#new-product-form').submit();
return false;
});
The data format is below:
[{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":false,"timeFrom":null,"timeTill":null},{"isActive":false,"timeFrom":null,"timeTill":null}]
How do I unserialize the above data in PHP?
Simple use json_decode for decode json data in php
<?php
$json = '[{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":true,"timeFrom":"9:00","timeTill":"18:00"},{"isActive":false,"timeFrom":null,"timeTill":null},{"isActive":false,"timeFrom":null,"timeTill":null}]' ;
echo "<pre>";
$converted = json_decode($json);
print_r($converted);
?>
I'm having difficulties grabbing any of the JSON information from this URL.
I've tried other JSON snippets and they seem to work so I'm not sure if it's the way that the URL is structured or something.
Basic example below.
<?php
$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);
echo "Body: " . $obj->Body;
?>
The link provided starts with
{ data :
which is valid javascript but invalid json. You can test it on http://jsonlint.com. To fix this we can replace the data with "data" :
$json = file_get_contents('http://nhs-sh.cfpreview.co.uk/api/version/fetchLatestData?dataType=Clinics&versionNumber=-1&uuID=website&dt=');
$obj = json_decode($json);
if (json_last_error() !== JSON_ERROR_NONE) { //check if there was an error decoding json
$json = '{ "data" :'. substr(trim($json), 8); // replace the first 8-1 characters with { "data" :
$obj = json_decode($json);
}
print_r($obj->data); //show contents of data
Please note that this fix is dependent on the data source e.g. if they change data to dataset. The correct measure would be to ask the developers to fix their json implementation.