[alibaba/fastjson]spring mvc中直接输出中文无异样, 且全程正常, 知道用fastjson输出却乱码

2025-11-03 814 views
7

方法调用: JSON.toJSONString(objVerifyResponse); 版本:fastjson-1.2.40

结果: { maxSeats: "9", msg: "??", routing: { adultPrice: 360, adultTax: 600, adultTaxType: 0, applyType: 0, childPrice: 280, childTax: 494, childTaxType: 0, currency: "RMB", data: "{"adultPrice":360,"adultTax":600,"adultTaxType":0,"applyType":0,"childPrice":280,"childTax":494,"childTaxType":0,"currency":"RMB","fromSegments":[{"aircraftCode":"737","arrAirport":"ICN","arrTime":"2017-11-27T12:05:00.000+09:00","cabin":"K","cabinClass":"1","carrier":"H1","codeShare":false,"depAirport":"TAO","depTime":"2017-11-27T09:30:00.000+08:00","flightNumber":"5885","stopCities":"TAO/ICN"}],"nationalityType":0,"priceType":0,"retSegments":[],"ticketInvoiceType":0,"ticketTimeLimit":120}", fromSegments: [ { aircraftCode: "737", arrAirport: "ICN", arrTime: "2017-11-27T12:05:00.000+09:00", cabin: "K", cabinClass: "1", carrier: "H1", codeShare: false, depAirport: "TAO", depTime: "2017-11-27T09:30:00.000+08:00", flightNumber: "5885", stopCities: "TAO/ICN" } ], nationalityType: 0, priceType: 0, retSegments: [ ], ticketInvoiceType: 0, ticketTimeLimit: 120 }, rule: { baggage: "???????", endorse: "???????", endorsement: 1, hasBaggage: 1, hasEndorse: 1, hasNoShow: 1, hasRefund: 1, noShowLimitTime: 1, partEndorse: 1, partEndorsePrice: 1, partRefund: 1, partRefundPrice: 1, penalty: 2, refund: "???????", specialNoShow: 0, status: 0 }, sessionId: "a713d7e1-0f39-4bf4-9279-7a925b2e59cd", status: 0 }

????号的都是中文输出

@RequestMapping( produces={"application/json;charset=UTF-8"}) 能解决问题, 但是JSON.toJSONString(objSearchResponse);不能在序列化的时候指定编码吗

回答

9

网页上右键,编码-选择UTF-8试试。

默认就是UTF-8,加上produces参数主要是通过response头告诉浏览器,后面的数据时UTF-8的。 你出现乱码是因为,浏览器不知道是什么编码。

2

@kimmking 您说的这些其实都已经试过了, 可能是和springmvc有一些冲突吧, 不管怎么设置, 其实最好还在序列化的时候直接有设置编码格式

6

JSON.toJSONString(objSearchResponse,"UTF-8" or "GBK");

我的意思是这种写法并不能解决问题。 有的浏览器是可能是a编码,也可能是b编码。

2

@kimmking 是的, 网页上右键,编码-选择UTF-8....... 这个我已经试过很多种变法方式

so, 这个也许不算是一个问题吧