Jobeso Jobeso - 1 year ago 80
JSON Question

PHP/Python json_decode doesn´t display special characters like €

I got a json which is generated in a python program and looks like this:

{"0": {"ausschreiber": "Beispiel; Zeitarbeit GmbH", "beschreibung": "\r\nF\u00fcr unseren Kunden suchen wir motivierte studentische Aushilfen auf flexibler Stundenbasis (450\u0080-Basis)", "datum": "17.11.2016", "name": "Studentische Hilfskr\u00e4fte gesucht", "email": ""}}

now i am decoding the json in my php program to get an associative array and display this on the website.
The Problem is that the special characters like the € char are not displayed but special chars like ö ä ü are displayed.
Here is the php program:

header('Content-Type: text/html; charset=utf-8');

function compare($old_data, $new_data){
$old_result = json_decode($old_data, true);
$new_result = json_decode($new_data, true);
echo $new_result[0]['beschreibung'];

function go4it(){
$db_data=json_content(); //creates the json from the Database
$crawler_data = file_get_contents('http://localhost/phppath/python_program.cgi'); //calls the cgi which returns the json
compare($db_data, $crawler_data);

What i tried:

  • set the header to utf-8

  • $new_result = json_decode(utf8_encode($new data), true);

  • iconv_set_encoding("internal_encoding", "UTF-8");

  • iconv_set_encoding("input_encoding", "UTF-8");

  • iconv_set_encoding("output_encoding", "UTF-8");

Thanks for your help!

so it seems like the issue is located in the python program, thanks to @FranzGleichmann . I think the problem is with the encoding of the page where i get the content from. The page says it is ISO-8859-1 so i tried this:

url = ''
source_code = requests.get(url)
plain_text = source_code.text
plain_text.decode('iso-8859-1', 'ignore').encode('utf8', 'ignore')

but then i get the error: "UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 8496: ordinal not in range(128)"

Answer Source

it was a problem with the python script

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download