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

My while loop is:
I have tried to insert json_decode and json_encode and also each one of them without the other, but have had no luck. I keep getting the error JSON.parse: unexpected non-whitespace character after JSON data

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";
}

I understand that the json_encode has to be out of the while loop but don't understand how to do this.
Any help is appreciated. Thank You.

The particular error you have cited (JSON.parse: unexpected non-whitespace character after JSON data) is when Javascript is decoding it.

json_encode returns a string from the mixed value argument. json_encode Doc

You would either need to echo out the result or add it to another variable to output later. eg.

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";
}

If you are trying to encode multiple rows and return them all, you would be better off adding the $result to an array and encoding that outside the while loop as if you don't, your JSON string could look like:

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

That is not valid JSON as it can only parse one object or array at once. Below is a valid JSON string

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

That is an array representation of your data and will parse into a list of your JSON encoded objects. Here is your code using an array to store the JSON before echoing it.

$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);
}

As in my example above you are echoing the string regarding selecting population or GDP, as that isn't JSON encoded and the part above would otherwise be encoded in JSON, you may have parse errors when trying to decode it. If this PHP page is meant to return JSON encoded data and your error message isn't JSON encoded, it is likely to have problems with whatever is getting the value.