PHP/Python json_decode不显示特殊字符,如€


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

我得到了一个json,它是在python程序中生成的,看起来像这样:

{"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": "info@hindi.de"}} 

现在我正在解码json在我的PHP程序,以获得一个关联数组,并显示在网站上。问题是不显示像€字符这样的特殊字符,而是显示像ö ä ü这样的特殊字符。下面是php程序:

<?php
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);
}
go4it();

What i tried:

  • 设置标题为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");

谢谢你的帮助!

编辑1 所以看起来问题是在python程序中,感谢@FranzGleichmann。我认为问题是与页面的编码,我从哪里得到的内容。页面显示它是ISO-8859-1,所以我试了这个:

url = 'https://www.example.com'
source_code = requests.get(url)
plain_text = source_code.text
plain_text.decode('iso-8859-1', 'ignore').encode('utf8', 'ignore')
print(plain_text.encoding)

但随后我得到错误:"UnicodeEncodeError: 'ascii'编解码器无法编码字符u''xf6'在位置8496:序数不在范围(128)"

python脚本有问题