-1

I am trying to convert my $response array to JSON but it is not converting. I tried to echo the values in the array to be sure that it is not null.

    if($row['count(*)']>0)
    {
        $sql="select * from guide_city_detail where CITY='$place'";
        $result = $conn->query($sql);
        $row = $result1->fetch_assoc();
        $row = array_map('utf8_encode', $row);
        $response['status']=1;
        $response['city']=$row['CITY'];
        $response['state']=$row['STATE'];
        $response['country']=$row['COUNTRY'];
        $response['heads']=$row['HEADS'];
        $response['col1']=$row['COL1'];
        $response['col2']=$row['COL2'];
        $response['col3']=$row['COL3'];
        $response['col4']=$row['COL4'];
        $response['col5']=$row['COL5'];
        $response['col6']=$row['COL6'];
        $response['col7']=$row['COL7'];
        $response['col8']=$row['COL8'];
        $response['col9']=$row['COL9'];
        //echo $response['country'];
    }
    else
    {
        $response['status']=0;// save the entry for future references
        $output = shell_exec("java -cp \".;D:/letsgo/mysql.jar;D:/letsgo/jsoup.jar;\" GeneralPopulate $place 2>&1");
        //echo "$output";
    }
    $res=json_encode($response);
    echo $res;
}
9
  • What's the output of $e = json_last_error();? Commented May 2, 2017 at 15:12
  • The output of $e = json_last_error(); is 5 Commented May 2, 2017 at 15:13
  • from php.net/manual/en/function.json-last-error.php: 5 = JSON_ERROR_UTF8 Commented May 2, 2017 at 15:14
  • In my system, 5 is JSON_ERROR_UTF8. You are not using UTF-8, are you? Commented May 2, 2017 at 15:15
  • Please see the updated question, I now converted to UTF-8 as suggested in the answer Commented May 2, 2017 at 15:24

1 Answer 1

0

If json_last_error gives you the JSON_ERROR_UTF8 error code (5), then you will have to encode your datas in UTF-8 :

Need to convert final array to utf-8.

<?php

$response = array_map('utf8_encode', $response);
Sign up to request clarification or add additional context in comments.

12 Comments

still not getting the json string
Does the error code is the same ?
How could you possibly know that data is currently encoded as ISO-8859-1?
Yes error code is same
This is getting kind of surrealistic. There's no "default format", you can fire any editor and save anything in a file with ".php" extension. Neither utf8_encode() nor mb_detect_encoding() do what most people assume by name.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.