Json传字符串中文问题解决方案

之前做项目时遇到了后台向前台传json时,中文乱码问题,后来得到解决

下面是代码
(项目中用到了阿里巴巴的包)
package com.neusoft.resume.minority.controller;

import java.util.List;

import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.neusoft.resume.minority.pojo.Minority;
import com.neusoft.resume.minority.service.IMinorityService;

@Controller
@RequestMapping(“user/minority”)
public class MinorityController {
@Resource
private IMinorityService minorityservice;

@RequestMapping(value="list",produces="html/text;charset=UTF-8")
@ResponseBody
public String GetMinority(){
    JSONObject json=new JSONObject();
    try{
        List minority = minorityservice.getAll();

        if (minority.size()>0) {
            json.put("error_num", 0);
            json.put("result", JSON.toJSONString(minority));

        } else {
            json.put("eror_num", 1002);
            json.put("error_msg", "no data");
        }

    }catch(Exception ex){
        json.put("error_num", 1000);
        json.put("error_msg", ex.getMessage());
    }
    return json.toJSONString();
}

}

这样前台写时,不会出现中文显示“?????”的问题

你可能感兴趣的:(java)