json 对象里面含有 =的解决办法

           今天通过restful 调用接口的时候,遇到这样的问题,通过接口返回的数据如下:

          

{
    "code": 0,
    "message": "成功",
    "result": {
        "count": 1
    }
}

  然后 需要获取 count后面的值,于是 通过json转化的方式:

  

 RestClient client = RestClients.createDefault();
            String host = SystemProperties.getInstance().getProperty("****l");;
            Integer strNum = 0;
            String url = host + "/restful/biProxyService/article/count";
            RestRequest request = new RestRequest(url);
            request.addParam("circleId", circleId);
            request.addParam("articleId",articleId);
            try{
                RestResponse<Object> response = client.post(request, Object.class);
                logger.info("返回数据:" + response);
                if(response.isSuccess()){
                    Object obj= response.getResult();
                    logger.info("obj:" + obj);
                    JSON json = (JSON)JSON.parse(obj);
                    JSONObject jsonObject = (JSONObject)json;
                    strNum = jsonObject.getJSONObject("result")==null?0:jsonObject.getJSONObject("result").getInteger("count");
                    logger.info("strNum");
                }
            }catch (Exception e){
                logger.error("调用bi获取文章数据异常",e);
            }
            return strNum;

  然后怎么都转不了,最后打印日志发现 response.getResult的结果是:

   

{code=0,message=成功,result={count:12}}

  额 ,然后报包json转化异常了,因为正常的json格式应该是:

{code:0,message:成功,result:{count:12}}

最后想到了google的gson,通过

   Gson gson = new Gson();

  gson.toJson(obj)

 转化为String对象,在通过fastjson转化 就好了。

完整代码:

  public Integer getArticleCount(String circleId,String articleId) {
            RestClient client = RestClients.createDefault();
            String host = SystemProperties.getInstance().getProperty("***");;
            Integer strNum = 0;
            String url = host + "/restful/biProxyService/article/count";
            RestRequest request = new RestRequest(url);
            request.addParam("circleId", circleId);
            request.addParam("articleId",articleId);
            try{
                RestResponse<Object> response = client.post(request, Object.class);
                logger.info("返回数据:" + response);
                if(response.isSuccess()){
                    Object obj= response.getResult();
                    logger.info("obj:" + obj);
                    Gson gson = new Gson();
                    String str = gson.toJson(obj);
                    logger.info(str);
                    JSON json = (JSON)JSON.parse(str);
                    JSONObject jsonObject = (JSONObject)json;
                    strNum = jsonObject.getJSONObject("result")==null?0:jsonObject.getJSONObject("result").getInteger("count");
                    logger.info("strNum");
                }
            }catch (Exception e){
                logger.error("调用bi获取文章数据异常",e);
            }
            return strNum;
        }
原文地址:https://www.cnblogs.com/thinkingandworkinghard/p/8550995.html