首页 > Web开发 > 详细

struts+json 返回json格式多了斜杠的问题

时间:2014-09-23 09:53:15      阅读:311      评论:0      收藏:0      [点我收藏+]

功能要求 C#做http请求 ,后台 struts+json 返回json格式数据, 大概代码是这样的

后台java

    public String jo ;
    
    public String getNumpsQty(){
                       //***  
JSONObject obj=new JSONObject(); JSONArray ja =JSONArray.fromObject(onv); obj.put("count", row); obj.put("root", ja); setJo(obj.toString()); System.out.println(jo); }catch(Exception e){ e.printStackTrace(); } return SUCCESS; }


struts  配置

<action name ="getOUTNumps" class ="Action.OUTServices"
             method ="getNumpsQty">
             <result type="json" name="success">
             <param  name ="root">jo</param>
             </result>
         </action>

接收到json 的格式

"{\"count\":6,\"root\":[{\"numPs\":\"T10140019\",\"qty\":\"500000\"},{\"numPs\":\"T10140020\",\"qty\":\"200000\"}]}"

 很明显格式有问题  多了个斜杠,无法解析
去google一下发现大神们的解释:

不能用String格式返回,String格式本身会加"" , 请求的json会经过两次序列化导致加了转义字符\

直接返回 JSONObject 就可以,

public JSONObject jo ;
    
    public String getNumpsQty(){
        
        JSONObject obj=new JSONObject();
        try{
//***
            List<OUTNumpsVo> onv=new ArrayList<OUTNumpsVo>();
            JSONArray ja =JSONArray.fromObject(onv);
            obj.put("count", row);
            obj.put("root", ja);
            setJo(obj);
            System.out.println(jo);
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return SUCCESS;
    }
    
    public JSONObject getJo() {
        return jo;
    }

    public void setJo(JSONObject jo) {
        this.jo = jo;
    }

 

struts+json 返回json格式多了斜杠的问题

原文:http://www.cnblogs.com/Marvellous/p/3987627.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!