我有一个while循环,在里面我想将PHP转换为Valid json

时间:2013-04-27 01:33:08

标签: php encode json

我的while循环是:
我试图插入json_decodejson_encode以及其中每一个都没有,但没有运气。我一直收到错误 JSON.parse:JSON数据后出现意外的非空白字符

while ($row = $res->fetchRow()){
    $resGold = $row['gold'];
    $resSilver = $row['silver'];
    $resBronze = $row['bronze'];
    $resGdp = $row['gdp'];
    $resPopulation = $row['population'];
    $resCountry = $row['country_name'];
    $gold_score = ($resGold * $gold_value);
    $silver_score = ($resSilver * $silver_value);
    $bronze_score = ($resBronze * $bronze_value);
    $score_pop = (($gold_score + $silver_score + $bronze_score)/$resPopulation);
    $score_gdp = (($gold_score + $silver_score + $bronze_score)/$resGdp);
    if($population == 'true'){ 
        $result = $res->fetchRow();
        $result['score'] = "$score_pop";
        json_encode($result);
    }
    else if($gdp == 'true'){ 
        $result = $res->fetchRow(); 
        $result['score'] = "$score_gdp"; 
        json_encode($result);
    }
}
if($population == 'false' && $gdp == 'false'){
    echo "Please select either population or gdp from view.htm";
}

我知道json_encode必须不在while循环中,但不明白如何执行此操作。
任何帮助表示赞赏。谢谢。

1 个答案:

答案 0 :(得分:1)

您引用的特定错误( JSON.parse:JSON数据之后的意外非空白字符)是Javascript解码时的错误。

json_encode从混合值参数返回一个字符串。 json_encode Doc

您需要echo输出结果或将其添加到另一个变量以便稍后输出。 例如

while ($row = $res->fetchRow()){
    $resGold = $row['gold'];
    $resSilver = $row['silver'];
    $resBronze = $row['bronze'];
    $resGdp = $row['gdp'];
    $resPopulation = $row['population'];
    $resCountry = $row['country_name'];
    $gold_score = ($resGold * $gold_value);
    $silver_score = ($resSilver * $silver_value);
    $bronze_score = ($resBronze * $bronze_value);
    $score_pop = (($gold_score + $silver_score + $bronze_score)/$resPopulation);
    $score_gdp = (($gold_score + $silver_score + $bronze_score)/$resGdp);
    if($population == 'true'){ 
        $result = $res->fetchRow();
        $result['score'] = "$score_pop";
        echo json_encode($result);
    }
    else if($gdp == 'true'){ 
        $result = $res->fetchRow(); 
        $result['score'] = "$score_gdp"; 
        echo json_encode($result);
    }
}
if($population == 'false' && $gdp == 'false'){
    echo "Please select either population or gdp from view.htm";
}

如果您尝试对多行进行编码并将它们全部返回,那么最好将$result添加到数组并在while循环之外进行编码,就好像不这样做一样JSON字符串可能如下所示:

{gold:123,silver:456,bronze:789}{gold:987,silver:654,bronze:321}

这不是有效的JSON,因为它只能一次解析一个对象或数组。下面是一个有效的JSON字符串

[{gold:123,silver:456,bronze:789},{gold:987,silver:654,bronze:321}]

这是数据的数组表示,并将解析为JSON编码对象的列表。以下是使用数组存储JSON的代码,然后回显它。

$results = array();
while ($row = $res->fetchRow()){
    $resGold = $row['gold'];
    $resSilver = $row['silver'];
    $resBronze = $row['bronze'];
    $resGdp = $row['gdp'];
    $resPopulation = $row['population'];
    $resCountry = $row['country_name'];
    $gold_score = ($resGold * $gold_value);
    $silver_score = ($resSilver * $silver_value);
    $bronze_score = ($resBronze * $bronze_value);
    $score_pop = (($gold_score + $silver_score + $bronze_score)/$resPopulation);
    $score_gdp = (($gold_score + $silver_score + $bronze_score)/$resGdp);
    if($population == 'true'){ 
        $result = $res->fetchRow();
        $result['score'] = "$score_pop";
        array_push($results,$result);
    }
    else if($gdp == 'true'){ 
        $result = $res->fetchRow(); 
        $result['score'] = "$score_gdp"; 
        array_push($results,$result);
    }
}
if($population == 'false' && $gdp == 'false'){
    echo "Please select either population or gdp from view.htm";
}
else
{
    //Note: This is in the 'else' statement as echoing the JSON then that string
    //      will also cause errors as it ends up not being valid JSON anymore
    echo json_encode($results);
}

如上面的示例所示,您回显有关选择填充或GDP的字符串,因为它不是JSON编码的,否则上面的部分将以JSON编码,您在尝试解码时可能会出现解析错误。如果这个PHP页面是为了返回JSON编码数据而你的错误消息不是JSON编码的,那么它可能会遇到任何有价值的问题。