Home > OS >  How to see the euro symbol in a json (PHP)
How to see the euro symbol in a json (PHP)

Time:10-10

I would like to take the euro symbol and print with a json, but I do not know why my code is not working. Here is my code:

<?php
   class DINHEIRO{

       private $valor;
       private $simbolo;

       public function setValor($valor){
           return $this->valor = $valor;
       }

       public function setSimbolo($simbolo){
           return $this->simbolo = $simbolo;
       }
       public function getValor(){
         return $this->valor;
       }
       public function getSimbolo(){
         return $this->simbolo;
       }
   }
   
   $value = new DINHEIRO();
   $value->setValor(6);
   $simbolo = "\xE2\x82\xAc";
   $value->setSimbolo($simbolo);
   echo $value->getSimbolo(); //Here is working
   echo json_encode(array('valorConvertido' => $value->getValor(), 'simboloMoeda' => $value->getSimbolo())); //Here is not working
   
?>

Here is the output: €{"valorConvertido":6,"simboloMoeda":"\u20ac"}

How to fix it?

CodePudding user response:

JSON is a format for storing and transmitting data from one system to another. To make sure strings aren't messed up during transmission, it represents non-printable and non-ASCII characters using escape sequences like the one you show. This is perfectly normal and fine.

When you come to actually use the data, you will turn it back into strings in whatever programming language you're using, and the actual Euro symbol will be there as expected.

  • Related