如何解决JSON返回的中文乱码?

如何解决JSON返回的中文乱码?,第1张

【 解决方法】:

@RequestMapping(value="/getphone",produces = "text/plaincharset=utf-8")

/**输入手机号码后判断手机号是否存在*/

@RequestMapping(value="/getphone",produces = "text/plaincharset=utf-8")

@ResponseBody

public String getphone(String phone,HttpSession session){

Users u=service.selectPhoneService(phone)

if(u==null){//如果为空,则需要注册

String str="请您先注册,再登录。"

session.setAttribute("str", str)

return "请您先注册,再登录。"

}

return "true"

【方法二,在spring-mvc.xml中添加】:

<!-- 处理请求返回json字符串的中文乱码问题 -->

<mvc:annotation-driven>

<mvc:message-converters>

<bean

class="org.springframework.http.converter.StringHttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>application/jsoncharset=UTF-8</value>

</list>

</property>

</bean>

</mvc:message-converters>

</mvc:annotation-driven>

python一行式解析json怎么避免中文转化为unicode编码?

在以 json 为数据传输格式的 RESTful 接口非常流行。为调试这样的接口,一个常用的办法是使用 curl 命令:

curl   http://xx.xx.xx.xx/some-restful-api

对于返回的 json 字符串,一般在服务端不加处理的情况下,都是没有任何 '\t' 和 '\n' 的。

为了方便查看,在 bash 上可以简单地对它进行格式化:

curl http://xx.xx.xx.xx/some-restful-api | python -m  json.tool

当然这要求机器上安装了 python,其实也就是利用了 json.tool 这个程序。

然而有时候还有一个问题,就是若返回的 json 字符串中包含中文,那么这样打印出来之后,中文会变成以 \u 开头的转义形式,从而让程序员无法直接观察到中文的内容,这并非是一个 bug,而是 json 本身的标准,它要求 json 的内容都是 ascii 编码的,标准的 json 编码器和解码器都会遵循这一点。

# vim /usr/lib64/python2.7/json/tool.py

# curl -s -X POST https://run.mocky.io/v3/e95f6c35-b3c8-43d9-b9ab-f5ce8c1054cf -H 'cache-control: no-cache' | python -m json.tool

python -m json.tool中文乱码问题

https://blog.csdn.net/twingao/article/details/105169997

python -m json.tool 中文乱码 Format JSON with python

https://www.cnblogs.com/ruiy/p/6525591.html

python -m json.tool to output Chinese

python -m json.tool to output Chinese

Saving utf-8 texts with json.dumps as UTF8, not as \u escape sequence

https://stackoverflow.com/questions/18337407/saving-utf-8-texts-with-json-dumps-as-utf8-not-as-u-escape-sequence


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9914095.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存