I have the following Json file:
[{"Datum":"21-07-2017","Zeit":"21:48","Menge":"545465476"},{"Datum":"21-07-2017","Zeit":"21:51","Menge":"78"},{"Datum":"21-07-2017","Zeit":"21:53","Menge":"456"}]
The Json validator says its ok.
But still I cannot this Json file via php_decode(). Instead I am getting "bool(false)"
My code
$jso = json_encode($jfile);
var_dump(json_decode($jso));
$error = json_last_error();
What am I doing wrong?
This script is saving an input to a txt database in Json format.
Your JSON encode and decode look correct. You can refer to the following resources for confirmation:
http://php.net/manual/en/function.json-decode.php
http://php.net/manual/en/function.json-encode.php
So the problem, most likely, lies in the way you get the json into the functions. In your case this is done with a variable "$jfile".
I suggest you do a var_dump of the $jfile variable to see what data it holds, if any.
My first guess, as others have pointed out as well, is that you're reading the json from a file (hence $jfile). This is usually done with the "file_get_contents" function which is described in detail here.
A working example
Your directory structure should look like this:
.
├── readjson.php
├── some_json_data.json
And your php code should look something like this:
<?php
$jfile = file_get_contents('some_json_data.json');
var_dump($jfile)
$jso = json_encode($jfile);
var_dump($jso);
$decoded_json = json_decode($jso);
var_dump($decoded_json);
$error = json_last_error();
?>
Note how I add more var_dump's and variables to explicitly monitor the state of the json objects. This might be helpful when learning new stuff.
I guess you have to first get the file into php, so $jso will hold json data (in terms of file contents)
$jso = file_get_contents('JSON_FILE_PATH');
//then simply use json_decode
var_dump(json_decode($jso));
Related
Sorry sorry sorry for the basic question but I've been stuck on this for a while and I am not sure what is up.
So I have a php file and a json file. When I run the php file on the terminal
php myFile.php
the output is identical to my json file.
the output looks like this:
[{"word":"now","score":"16","sentence":" Now, at the least calcu- lation,
this broken vase dates back two thousand five hundred years "}]
(the Json file also looks like the above)
When I pass in the json file to d3.json, I am able to see the visualization and all of it's data no-problem. But for some reason, when I replace the json file with the php file I am getting the error
Cannot read property 'length' of undefined
at Object.a [as extent]
so I am never getting or displaying the data. I've made sure that the php file is in the same directory, no typos etc. And I've checked the echo for any differences but there are none.
Thank you for your time!
my js looks like this:
//can replace myFile.php with json file and it works!
d3.json('myFile.php',
function (error,myData) {
// a bunch of stuff})
Thank you!
Convert your data to json using PHP's built in function json-encode
Then return the variable to d3.js.
I'm trying to use the data.gov.uk to grab the court data and do cool stuff with it. Problem I am having is if I try to decode the version from the server in php: https://courttribunalfinder.service.gov.uk/api.html it just won't load. It only works if I download the courts.json file and decode from that. However then I'm unable to query the data with something like courts.json?q=bristol say.
So my question is, should I be able to decode directly from the gov website or do I need to do it locally, and if that's the case how would I allow me to feed query strings to my local json file?
e.g. I want to be able to do something like:
$jsonurl = directory() . '/courts.json?q=old+bailey';
$json = wp_remote_get($jsonurl,0,null,null);
$court = json_decode($json['body'], true);
well,
There are two options for you
1) Use cURL Library as mentioned by Joanvo
2) use YQL(Yahoo Query Language) that is also another powerful thing around.
I am making a website to display the data at https://api.captcoin.com/address/top/100. I need to be able to make the website take variables("address", "percent", "balance", and "rank") from this script and make them local variables in my site so I can display them. How can I take these variables and use them in my site?
First you need to get the remote page contents:
$remote = file_get_contents('link');
Then, since the data is in json format, you need to decode it using json_decode function.
$data = json_decode($remote, true);
true means that $remote should be decoded as associative array.
And finally you can access the data like an ordinary php array:
echo $data['top'][0]['address'];
Also, you should add some logic to handle situations when remote server is not accessible.
Use json_decode to convert the content of that url into an array and then search through it like you would through any array.
To get the actual content of the site please refer to this post Get file content from a URL?
You can either do it with javascript or php.
With javascript use this:
http://api.jquery.com/jquery.getjson/
You take the pages output and push them as variables to the php.
With php you can use
http://php.net/manual/en/function.json-decode.php
You make an array to push the json data into:
$object = array();
$json = file_get_contents('https://api.captcoin.com/address/top/100');
$object = json_decode ( string $json , true)
Be aware this is untested and read the json_decode api to customize the function-call.
I'm using HTTPful to send some requests in PHP and get data in JSON, but the library is converting the result into objects, where I want the result to be an array. In other words, its doing a json_decode($data) rather than json_decode($data, true).
There is, somewhere, an option to use the latter, but I can't figure out where. The option was added in v0.2.2:
- FEATURE Add support for parsing JSON responses as associative arrays instead of objects
But I've been reading documentation and even the source, and I don't see the option anywhere... The only way I can think of is making my own MimeHandlerAdapter which does a json_decode($data, true) but it seems like a pretty backwards way of doing it if there is an option somewhere...
It may be a little late to answer this, but I did a little research while using Httpful and found the answer. Httpful uses a default set of handlers for each mime type. If one is registered before you send the request, it will use the one you registered. Conveniently, there is an Httpful\Handlers\JsonHandler class. The constructor takes an array of arguments. The only one it uses is $decode_as_array. Therefore, you can make it return an array like this:
// Create the handler
$json_handler = new Httpful\Handlers\JsonHandler(array('decode_as_array' => true));
// Register it with Httpful
Httpful\Httpful::register('application/json', $json_handler);
// Send the request
$response = Request::get('some-url')->send();
UPDATE
I realized that it sometimes parses the response into a funky array if you don't tell the request to expect JSON. The docs say it's supposed to work automagically, but I was having some issues with it. Therefore, if you get weird output, try explicitly telling the request to expect JSON like so:
$response = Request::get('some/awesome/url')
->expects('application/json')
->send();
I never used this library. But in a research I found that you can find this option at src/Httpful/Handlers/JsonHandler.php on line 11.
There you will see:
private $decode_as_array = false;
And this flag is used at the same file on line 27:
$parsed = json_decode($body, $this->decode_as_array);
You have to set decode_as_array to true value, to do this:
\Httpful\Httpful::register(\Httpful\Mime::JSON, new \Httpful\Handlers\JsonHandler(array('decode_as_array' => true)));
before Request::get calling
I'm passing a JSON object from flash AS3 to PHP, which is then taken apart and passed to the DB.
In Flash:
var jsonObject:Object = JSON.encode(currentlySelectedArray);
In PHP:
$json_pieces_array = $_POST['jsonArray'];
$json_obj = json_decode($json_pieces_array, true);
When I test my code by copy/pasting the output of 'trace (saveDataJSON.ToString());' and putting it into my '$_POST['jsonArray'] = '[[Valid JSONLint checked JSON here.]]', everything works fine and it gets pushed to the database.
But when I don't meddle and use the flash-sent $_POST, nothing pushes to the MYSQL DB.
My question is twofold:
1) What's the best way to bug test this sort of complication? I'm in the Flash interface.
2) What sorts of things should I be looking for? I already checked that the JSON being encoded was valid. Is there some sort of weird typecasting I'm missing?
At first you have to debug request from flash. You can do it in several ways:
create dummy file that does var_dump($_REQUEST);;
add file_put_contents('my_dump_file.txt', var_export($_REQUEST, true)); in existing script;
check your webserver logs;
debug you script with debugger (xdebug or similar).
Then you should check your PHP script. Try to var_dump($_REQUEST); on the top and exactly before json_decode. It can be so that $_POST['jsonArray'] is overwritten somewhere else.
I guess that the problem is between flash and php. In most cases there is simply misspelling or $_POST is mixed with $_GET.