2

What's wrong with the JSON available at the link below???

http://bruceexpress.com/beerstore/test/getottawaeaststorebeerlist.php

Using PHP, I generated the above JSON from an SQL query and encoded it with json_encode.

$retval = mysqli_query($connection, $sql);
if(! $retval )
{
    print('Could not select: ' . mysqli_error());
}
$storearray = array();
while($row = mysqli_fetch_assoc($retval))
{
    $storearray[] = $row;
}
header('Content-Type: application/json');
echo json_encode($storearray);

In SWIFT:

json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)

is throwing the NSCocoaErrorDomain Code=3840 exception. "JSON text did not start with array...."

I caught the exception and printed the data, and I am seeing this:

("<!DOCTYPE html>\n[{\"beer_id\":\"1650\",\"store_id\":\"4618\"},{\"beer_id\":\"1650\",\"store_id\":\"4607\"},{\"beer_id\":\"1650\",\"store_id\":\"4616\"},{\"beer_id\":\"1650\",\"store_id\":\"4604\"},{\"beer_id\":\"5213\", 

.......... ( etc. I didn't copy the whole thing. Its too long.")

I know there are many questions like this, but I am still not seeing the answer I need. What is wrong my JSON???

bhardy
  • 83
  • 10
  • 3
    There shouldn't be a doctype in the json; have you set the headers correctly as well (like so: http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script)? – chrisjlee Dec 02 '15 at 18:55
  • What else does your PHP script echo/print/output? – Mark Baker Dec 02 '15 at 18:56
  • 1
    you are printing ` ` to the page before you echo your `JSON` – cmorrissey Dec 02 '15 at 18:57
  • Also your headers are set to text/html – Liam Sorsby Dec 02 '15 at 18:58
  • Yes, I was wondering about the doctype. How do I get rid of that? There is nothing else echoing. I showed all relevant php code. I'm not printing on purpose. That is automatic. How do I stop it. – bhardy Dec 02 '15 at 18:58
  • 3
    `I showed all relevant php code.` My guess is that it is the non-relevant code that is sending that out. Check that any included files are not outputting anything. And I'm sure that it is only automatic because you have some script somewhere saying `echo " ";` – Jonathan Kuhn Dec 02 '15 at 18:59
  • 2
    I am a freakin' idiot! is on the first line of the php script. No echo in front of it, but its there. Stupid me! Thanks guys! – bhardy Dec 02 '15 at 19:04
  • It happens sometime. That's why stackoverflow has been made to point our silly mistakes. :) +1 – Pratik Soni Dec 03 '15 at 16:39

1 Answers1

0

At first encode your php file in UTF-8 without BOM. and delete all whitespaces before <?php tag Secondly dont print anything before if you want to send header; Thirdly you real need some check and return in your script, because i see now at least 2 bugs which will appear in a future

  • The UTF-8 without BOM does not apply in this case. As I acknowledged above, it was a " " sitting at the top of the script which I had copied and pasted from another file. My stupidity! As for the bugs, yes, I'll deal with that later, however I am generating a database for my own needs. Therefore, I control its structure fully and can be a little more lenient with my code. This is prep for a weekly inventory check that matches all beers against all beer stores in Ontario, and than shoves this data into my database. However, first I have to scan the html and extract the data. – bhardy Dec 02 '15 at 19:35
  • Its ok, Sergey. Only the Doctype was the issue. All is well now, and I have successfully generated all of the store/beer pairs I need. Wish there was a faster way to do this, but now I have to open and then scan the html for every store/beer pair. For example : http://www.thebeerstore.ca/beers/inventory/1650/4618 – bhardy Dec 02 '15 at 20:00