-2

I wan't decode json string from file, but there is problem with special characters(for ex. '™', '★'..). My code from PHP

$price_string = file_get_contents("/var/www/bot/price.json");
$price = json_decode($price_string, true);
echo $price["StatTrak™ P250 | Mehndi (Field-Tested)"];

Result should be 12.01, but the result is this:

Notice: Undefined index: StatTrak� P250 | Mehndi (Minimal Wear) in /var/www/html/menu.php on line ..

And the JSON string is for example:

{
    "★ Huntsman Knife | Boreal Forest (Well-Worn)": 62.84,
    "StatTrak™ P250 | Mehndi (Field-Tested)": 12.01
}
Alex
  • 61
  • 3
  • 9

1 Answers1

0

Aside from very bad idea of having such random keys, it's mostly because of encoding mismatch. Checklist:

  • ensure your PHP script file is using the same encoding your JSON file is (most likely UTF8)
  • ensure your PHP code is not processing the key string prior accessing the array and if it does, ensure your PHP is internally also using UTF8 (should by default, but that's configurable)
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141