json encoding € symbol

2019-08-15 07:57发布

问题:

I have a php page generating json extracting values from a mysql table. Everything is fine except where I have values containing euro symbol (like "€ 20,00") the euro symbol show up in my json as \u20ac...

Like this:

[
  {
    "id": "535",
    "Name": "Pluto",
    "fare": "\u20ac 20,00"
  }
]

In the page evaluating this json it appears as "€ 20,00", so this is fine, but I really would like it to appear the same also in the json string itself.

Any hint?

回答1:

Multibyte can - as long as they are not control characters - be displayed literally or escaped as \uXXXX and both representations are correct. If you prefer the literally representation and if you use php to create the json then you need to pass the correct options to json_encode: Parameters

The option you are looking for is JSON_UNESCAPED_UNICODE:

Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0



回答2:

this is a very simple example. I can complete it when i know what do you want exactly.

var json = [
  {
    "id": "535",
    "Name": "Pluto",
    "fare": "\u20ac 20,00"
  }
];

$(function(){$('#screen').html(json[0].fare);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id='screen'></div>

this example is more perfect: https://jsfiddle.net/a3dmorteza/x7fwto5L/