Asked  7 Months ago    Answers:  5   Viewed   37 times

I have a very simple bit of code

$pc1 = $_POST['post_code1'];
$pc2 = $_POST['post_code2'];
$url = "".$pc1."%20to:".$pc2;
$url_data = file_get_contents($url);
$json_data = json_decode($url_data);

$url_data is full of juicy json stuff but $json_data returns NULL. Does anyone have an idea why?



I found the following worked after find a number of people with similar problems

$json_data = json_decode(utf8_encode($url_data),true);


Saturday, May 29, 2021
answered 7 Months ago

What a HORRENDOUS debug session.. well there's good news.. I figured it out..

I started looking at it using AJAX and logging it with Firebug... and it turns out json_decode (or eval by the way) cannot handle ", which is what PHPUnit sends back (Come on Sebastian!), so to fix it:

$json = str_replace('"', '"', $json);

Now I thought they were the same.. maybe someone can enlighten me..

Wednesday, March 31, 2021
answered 9 Months ago

json.loads() takes a JSON encoded string, not a filename. You want to use json.load() (no s) instead and pass in an open file object:

with open('/Users/JoshuaHawley/clean1.txt') as jsonfile:
    data = json.load(jsonfile)

The open() command produces a file object that json.load() can then read from, to produce the decoded Python object for you. The with statement ensures that the file is closed again when done.

The alternative is to read the data yourself and then pass it into json.loads().

Thursday, September 9, 2021
answered 3 Months ago

Add this in place of jObj = new JSONArray(result.toString());

JSONObject obj = new JSONObject(result.toString());
JSONArray arr = obj.getJSONArray("value");

Now you can use JSONArray arr the way you want.

Tuesday, October 19, 2021
answered 2 Months ago

Debugging suggestion:

Check the output of json_last_error(). It should give you an exact reason why it doesn't work. Available from PHP 5.3.0 only, though.

The reason:

JSONP is not identical with JSON. It contains extra data that breaks json_decode().


Remove the extra brackets using substr($AVDecode, 1, strlen($AVDecode)-2)

Friday, October 22, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :