为什么json无效?

时间:2014-10-20 08:08:54

标签: php jquery json mysqli wysihtml5

我想用jquery创建一个json请求来填充wysihtml5 textarea但它不起作用

$("#calendarAnchor").click(function(event) {
            $.getJSON( "php/databaseHandler.php?action=GET_DATE_DETAILS&day=1&month=11&year=2014&username=bd107a66ba", function( json ) {
                alert( "JSON Data: " + json ); // not executed
            });             
        });

当我将json放入jsonlint时,它不会验证

({'id': '1124','day': '1','month': '11','year': '2014','red_day': 'ja','name_day': ['Allhelgona'],'images':[{'id': '2','url': 'svala_mini15.png','description': 'Idag är det soligt','category_id': '1','slot_id': '0'},{'id': '1','url': 'img/arstider/host/Flyttfagel_sadesarla_mini15.png','description': 'Idag regnar det, men vi har kul ändå!','category_id': '1','slot_id': '1'}],'holidays':[{'id': '1','name': 'Allhelgon','description': 'Nu firar man!'}],'dateDescription':[{'description': 'Idag!'}]})

WWhy是json无效吗?我应该更改输出还是使用jquery以其他方式读取字段?输出json的php是

function getDateDetails($day, $month, $year, $username, $db){
$sql = <<<SQL
SELECT calendar_dates.id, 
calendar_dates.day, 
calendar_dates.month, 
calendar_dates.year,
calendar_dates.name_day,
calendar_dates.red_day 
FROM calendar_dates 
WHERE calendar_dates.day=$day 
AND calendar_dates.month=$month 
AND calendar_dates.year=$year
SQL;

    $ret = "{";
    $isFirst = true;

    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }

    while($row = $result->fetch_assoc()){
        if($isFirst){
            $names = join("','", array_filter(explode(";", "'" . $row['name_day'] . "'")));
            $imagesJson = getImagesJson($row['id'], $username, $db);
            $holidaysJson = getHolidaysJson($row['id'], $username, $db);
            $dateDescriptionJson = getDateDescriptionJson($row['id'], $username, $db);

            $ret .= "'id': '" . $row['id'] . "',";
            $ret .= "'day': '" . $row['day'] . "',";
            $ret .= "'month': '" . $row['month'] . "',";
            $ret .= "'year': '" . $row['year'] . "',";
            $ret .= "'red_day': '" . $row['red_day'] . "',";
            $ret .= "'name_day': [";


            $ret .= $names;             
            $ret .= "],";
            $ret .= "'images':";
            $ret .= $imagesJson . ",";
            $ret .= "'holidays':";
            $ret .= $holidaysJson . ",";
            $ret .= "'dateDescription':";
            $ret .= $dateDescriptionJson . ",";
            $isFirst = false;
        }

    }

    $ret = rtrim($ret, ",");
    $ret .= "}";

    return($ret);
}

更新

在Dave的帮助下,这是有效的json

{
    "id": "1124",
    "day": "1",
    "month": "11",
    "year": "2014",
    "red_day": "ja",
    "name_day": [
        "Allhelgon "
    ],
    "images": [
        {
            "id": "2",
            "url": "mini15.png",
            "description": "Idag är det soligt!",
            "category_id": "1",
            "slot_id": "0"
        },
        {
            "id": "1",
            "url": "sadesarla_mini15.png",
            "description": "Idag regnar det!",
            "category_id": "1",
            "slot_id": "1"
        }
    ],
    "holidays": [
        {
            "id": "1",
            "name": "Allhelgon",
            "description": "Nu!"
        }
    ],
    "dateDescription": [
        {
            "description": "Idag!"
        }
    ]
}

我的新php(仍然从users.username = '$username'易受攻击?)是

function getDateDescription($dateId, $username, $db){
$sql = <<<SQL
SELECT calendar_date_description.description
FROM calendar_date_description
INNER JOIN calendar_users
ON calendar_users.username = '$username'
WHERE calendar_date_description.user_id= calendar_users.id
AND calendar_date_description.date_id = $dateId
SQL;

$ret = "[";
$description ="";
    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }

    while($row = $result->fetch_assoc()){
        $description = $row['description'];
    }

    return($description);   
}

function getDateDetails($day, $month, $year, $username, $db){

$sql = <<<SQL
SELECT calendar_dates.id, 
calendar_dates.day, 
calendar_dates.month, 
calendar_dates.year,
calendar_dates.name_day,
calendar_dates.red_day 
FROM calendar_dates 
WHERE calendar_dates.day=$day 
AND calendar_dates.month=$month 
AND calendar_dates.year=$year
SQL;

    //$ret = "{";
    $isFirst = true;

    if(!$result = $db->query($sql)){
        die('There was an error running the query [' . $db->error . ']');
    }

$ret = array();

while ($row = $result->fetch_assoc()) {
    $names               = array_filter(explode(";", $row['name_day']));
    $dateDescriptionJson = getDateDescription($row['id'], $username, $db);

    $ret = array(
        'id'              => $row['id'],
        'day'             => $row['day'],
        'month'           => $row['month'],
        'year'            => $row['year'],
        'red_day'         => $row['red_day'],
        'name_day'        => $names,
        'dateDescription' => $dateDescriptionJson
    );
}

$ret = json_encode($ret);

return ($ret);
}

1 个答案:

答案 0 :(得分:1)

我很乐意帮助,但实际上我推荐了json_encode。问题来自单引号,但潜在的问题是你手工构建这个复杂的JSON字符串。有时我会这样做,但仅在非常罕见的情况下,例如一级JSON字符串{"error":"true"},但对您来说情况并非如此。

请尝试使用此方法:

$ret = array();

while ($row = $result->fetch_assoc()) {
    $names               = array_filter(explode(";", $row['name_day']));
    $imagesJson          = getImages($row['id'], $username, $db);
    $holidaysJson        = getHolidays($row['id'], $username, $db);
    $dateDescriptionJson = getDateDescription($row['id'], $username, $db);

    $ret = array(
        'id'              => $row['id'],
        'day'             => $row['day'],
        'month'           => $row['month'],
        'year'            => $row['year'],
        'red_day'         => $row['red_day'],
        'name_day'        => $names,
        'images'          => $imagesJson,
        'holidays'        => $holidaysJson,
        'dateDescription' => $dateDescriptionJson
    );
}

$ret = json_encode($ret);

return ($ret);

高度建议保留PHP数组格式的所有内容,然后当数据准备好输出到客户端时,再做一个最终的json_encode。

所以这意味着:

getImagesJson       -> getImages
getHolidaysJson     -> getHolidays
dateDescriptionJson -> dateDescription

然后这些函数将返回PHP数组而不是JSON。这将允许您更轻松地跨函数操作数据。只需将相同的技术应用于这些功能,这将使您的生活变得更加轻松。