-2

以下 PHP 返回一个无效的 json:

$sql = 'select * from website where websiteID = "'. $websiteID .'"';
$q = mysql_query( $sql ) or die(mysql_error());
$res = json_encode( mysql_fetch_assoc($q) );
echo $res;

这是从浏览器复制的json:

{"websiteID":"162","userID":"235","URL":"coucou","themeID":"1","telephone":"","websiteTitle":"","description" :"","keywords":"","nomEntreprise":"","adresse":"","email":"","Page1Title1":"parameter1","Page1Title2":"parameter2","Page1Title3 ":"parameter3","Page1Title4":"parameter4","Page1Title5":"parameter5","page1Title6":"parameter6","page1Title7":"parameter7","page1Title8":"parameter8","page1Title9": "参数9","page1Title10":"parameter10","Page1Text1":"parameter11","Page1Text2":"parameter12","Page1Text3":"parameter13","Page1Text4":"parameter14","Page1Text5":"parameter15","Page1Text6":"parameter16" ,"Page1Text7":"parameter17","Page1Text8":"parameter18","Page1Text9":"parameter19","Page1Text10":"parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20 parameter20参数20 参数20 参数20 参数20 参数20 参数20 参数20 ","Page1Text11":"parameter21","Page1Text12":"parameter22","Page1Text13":"parameter23","Page1Text14":"parameter24","Page1Text15":"parameter25","Page1Text16":"parameter26","Page1Text17":"", "Page1Text18":"","Page1Text19":"","Page2Title1":""}

如何解决此问题并传递有效的 json 输出?


显然json是有效的。出于某种原因,从浏览器复制到http://jsonlint.com/时出现错误。

4

2 回答 2

0

它是有效的 JSON。我在http://jsonlint.com/上尝试过,它解析得很好。

也被完美地解析为 JavaScript 变量:http: //jsfiddle.net/FdYE6/

于 2012-09-03T11:07:06.317 回答
0

Three (possible) issues, only one directly related to your question:

  • json_encode expects utf-8 encoded data. The dataset you've posted in your question doesn't seem to contain "problematic" characters (it all seems to be within the ascii7 range and the utf-8/ascii7 have the same representation of characters). Still this could be an issue. You have to set the mysql connection charset to get utf-8 encoded data from the mysql server.
  • you put the parameter as a string literal into the sql statement. Then you have to take care of sql injections, i.e. you have to encode/escape the actual parameter value properly. Even if you are (at the moment) sure that $website cannot contain characters that may break the query.
  • the mysql exteion is marked as deprecated, better use another api/modlue to communicate with the mysql server, e.g. mysqli or pdo

Still, if your php-mysql module supports the function mysql_set_charset() it's possible to get it to work with the old mysql extension. But you hae to do this on both ends: when you insert/update data and when you retrieve data. see https://dev.mysql.com/doc/refman/5.1/en/charset.html

<?php
$mysql = mysql_connect('localhost', 'localonly', 'localonly')
if ( !$mysql ) {
    youErrorHandler($mysql);
}
$rc = mysql_set_charset('utf8', $mysql);
if ( !$rc ) {
    youErrorHandler($mysql);
}

$sql = sprintf(
    "
        SELECT 
            *
        FROM
         website
        WHERE
            websiteID = '%s'
        LIMIT
            1
    ",
    mysql_real_escape_string($websiteID, $mysql)
);
$q = mysql_query( $sql, $mysql ) or die(mysql_error($mysql));
$row = mysql_fetch_assoc($q);
if ( !$row ) {
    $row = array('error'=>'no result');
}
$res = json_encode($row);
于 2012-09-03T11:18:45.090 回答