Output to PHP JSON Web Service charset UTF-8

I host the web service in the PHP JSON release.

I have a Hebrew dataset in a database, and I'm sending this as output to a web service.

When I send data initially, it outputs the result as follows:

JSON:

{ "tasklist": [ { "customerID": "9936", "name": "טר ×רמ×" ×™×–×•× ×•×'×™× ×•×™ ×'×¢"מ", "cargo":"××'רר", "destination":"מכר", "quantity":"1.000000", "startdate":"03/01/201300: 00: 00" } ] } 

But this "××'רר" may be available for analysis by the Android / Iphone parser and convert it to the original Hebrew. But I encountered an error in the " "name": "טר ×רמ×" ×™×–×•× ×•×'×™× ×•×™ ×'×¢"מ", . where " is between the line, so JSON is invalid and shows an error!

enter image description here

To abort this problem, I used UTF-8 to convert this "××'רר" this to Hebrew "נברר" . But in this case, the problem remains the same:

PHP:

header ('Content-type: text / html; charset = UTF-8');

JSON:

 { "tasklist": [ { "customerID": "9936", "name": "טר ארמה יזום ובינוי בע"מ", "cargo":"נברר", "destination":"מכר", "quantity":"1.000000", "startdate":"03/01/201300: 00: 00" } ] } 

But the problem remains:

enter image description here

Also in some cases I get this due to using UTF-8

 "name":"מחצבות כפר גלעדי-חומרי מ " 
  • How can I solve this problem?
  • Is there any other specific code I need to use?

Note. Data cannot be modified in the database. The solution should be at the time of the output in JSON.

How the data stored in the database is displayed:

name

× × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × × /

My PHP Script that outputs JSON:

 <?php //My DB connection and Query comes here $jsontext = '{"tasklist":['; while($row = mysql_fetch_array($queryExe)){ $jsontext .= '{"customerID":"'.$row['AUTO_ID'].'",'; $jsontext .='"name":"'.$row['Customer_Name'].'",'; $jsontext .='"cargo":"'.$row['Type_of_Cargo'].'",'; $jsontext .='"destination":"'.$row['Destination'].'",'; $jsontext .='"quantity":"'.$row['Quantity'].'",'; $jsontext .='"startdate":"'.$row['startdate'].'"},'; } $jsontext = substr_replace($jsontext, '', -1); // to get rid of extra comma $jsontext .= "]}"; header('Content-type: text/html; charset=UTF-8'); //Output the final JSON echo $jsontext; ?> 

Thanks for your help in advance!

Was the question clear? to understand my problem.

+6
source share
3 answers

If your db field is utf8, you should make a fist:

 mysql_query("SET NAMES 'utf8'"); 

You should always do "SETTINGS NAMES ..." before inserting your data. Make sure you really save utf8 encoded strings!

then execute your query:

 mysql_query($your_query); $array = array("tasklist"=>array()); while($row = mysql_fetch_array($queryExe)){ $a = array(); $a["customerID"] = $row['AUTO_ID']; $a["name"] = $row['Customer_Name']; $a["cargo"] = $row['Type_of_Cargo']; $a["destination"] = $row['Destination']; $a["quantity"] = $row['Quantity']; $a["startdate"] = $row['startdate']; $array["tasklist"][] = $a; } header("Content-type: application/json; charset=utf-8"); echo json_encode($array); exit(); 

I realized that this is not enough if the default encoding for the servers is iso. In this case, I need to do the following in my .htaccess:

 AddDefaultCharset utf-8 
+15
source

You must change your code to use json_encode. You need to pass it correctly encoded utf8 data.

If you use MySQL, you can try the following before the query to get your data.

 SET NAMES 'utf8'; 

You can also learn utf8_encode .

+1
source

From http://www.php.net/manual/en/function.json-encode.php#100565

However, quotes will result in invalid JSON, but this is only a problem if you use json_encode () and just expect PHP to magically avoid your quotes. You need to do the escalation yourself.

Maybe you can replace " with \" , I think this will solve the problem.

Source: PHP JSON String, escape Double Quotes for JS output

+1
source

Source: https://habr.com/ru/post/950869/


All Articles