2014-08-31 310 views
1

我有Spring控制器方法,它使用Google.Gson返回json字符串。问题是,我的json字符串有UTF-8内容,当我从浏览器访问url时,它显示编码格式的数据。json的UTF 8解码问题(使用谷歌gson)字符串

@ResponseBody 
@RequestMapping(value="/getLocations.json", method = RequestMethod.GET) 
public String getLocations(HttpServletRequest request,HttpServletResponse response)throws Exception { 
    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create(); 
    response.setContentType("application/json; charset=utf-8"); 
    Set<Locations> locs = channelService.getLocations(); 
    String json = gson.toJson(locs);   
    return json; 
} 

当我从下面的浏览器地址栏访问getLocations.json是我所得到的:

[{"id":1,"name":"\u0026#3248;\u0026#3262;\u0026#3255;\u0026#3277;\u0026#3231;\u0026#3277;\u0026#3248;\u0026#3264;\u0026#3247;"},{"id":2,"name":"Districts"}] 

任何想法,我会拨错?或者我怎样才能解码数据?

+0

尝试删除'字符集= UTF-8' – 2014-08-31 15:22:35

+0

@ ANKUR - 辛格尔,它仍然是相同的 – Praveen 2014-09-01 01:15:30

回答

0

试试这个

var uri = "http://w3schools.com/my test.asp?name=ståle&car=saab"; 
var uri_enc = encodeURIComponent(uri); 
var uri_dec = decodeURIComponent(uri_enc); 
var res = uri_enc + "<br>" + uri_dec; 


http%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab  // Encoded URI 
http://w3schools.com/my test.asp?name=ståle&car=saab       // Decoded URI 

解码后,尝试解析JSON

var json = '{"result":true,"count":1}', 
    obj = JSON.parse(json);