1

I created a php file to generate JSON code.

My php code is:

<?php
include ('config.php');

mysql_query('SET CHARACTER SET UTF8');
mysql_query("SET NAMES utf8; ");

$check = mysql_query("SELECT * FROM news WHERE 1");

while($row=mysql_fetch_assoc($check))
$output[]=$row;

    $json_encode =json_encode($output);
    $utf8_decode = utf8_decode($json_encode);
    echo $json_encode;
    mb_convert_encoding($json_encode, 'UTF-8');
    $html_entity_decode = html_entity_decode($json_encode);

mysql_close();
?>

and the output is in this link: here

But when I check the validation of the JSON format in this site: here

It gives me the following error:

The JSON input is NOT valid according to RFC 4627 (JSON specification). Unexpected token [{"news_id":"11","news_content":"\u0639\u0637\u0648\u0631 \u0628\u0623\u062e\u0641 \u0627\u0644\u0627\u062d\u062c\u0627\u0645 \u062a\u0635\u0644\u062d \u0644\u062c\u0645\u064a\u0639 \u062...

And this affects the parsing in Java and gives me the following Exception:

Error parsing data org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray

How to solve this issue?

Thanks in advance.

Eman87
  • 2,735
  • 7
  • 36
  • 53
  • 1
    Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 30 '14 at 20:38
  • Thanks for your suggestion, but I need now to solve the error. Is your suggestion will prevent the error in Json ? – Eman87 Sep 30 '14 at 21:03
  • No, but will prevent future problems with MySQL. – Jay Blanchard Sep 30 '14 at 21:09
  • Your JSON output validates here - http://jsonlint.com/ It also validates in the link you provided. – Jay Blanchard Sep 30 '14 at 21:11
  • Haha, that string does in fact validate. I think you're using the mouse to copy and if you're using view-source or something else along those lines, some kind of white character is being put in the front. If I copy select carefully at the first letter while viewing source, that javascript validates. I've replicated it several times now. – Paul Carlton Sep 30 '14 at 21:11
  • @Jay Blanchard it gives me `Unexpected token` I don't know whats wrong and I'm disappointed. :( – Eman87 Sep 30 '14 at 21:32
  • I'm sorry you're disappointed but it is good to know that your JSON validates....right? You may be copying something incorrectly. – Jay Blanchard Sep 30 '14 at 21:33
  • I tried the same thing in other server and it is valid, see this link: http://www.giga-soft.com/fredericm_mobile/news.php? It is the same php file – Eman87 Sep 30 '14 at 21:41
  • Actually... try taking out the ending ?> tag... also try adding header("Content-Type: 'application/json; charset=UTF-8"); to the top of the page – Paul Carlton Sep 30 '14 at 21:59

0 Answers0