json encoding € symbol

*爱你&永不变心* 提交于 2020-01-05 05:37:07

问题


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/



来源:https://stackoverflow.com/questions/37372421/json-encoding-%e2%82%ac-symbol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!