Java使用jackson问题解决
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
蕃薯耀 2015年9月19日 14:50:37 星期六
http://fanshuyao.iteye.com/
一、描述
使用jackson处理查询淘宝手机号码归属返回的json数据时,发现程序报错。
二、淘宝手机号码归属查询接口
(https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13430878244)
返回的数据如下:
__GetZoneResult_ = { mts:'1343087', province:'广东', catName:'中国移动', telString:'13430878244', areaVid:'30517', ispVid:'3236139', carrier:'广东移动'}
三、错误一:
在标准json中,要求键和值都要用双引号("")包括的,而淘宝返回的值用单引号,所以报错。
com.fasterxml.jackson.core.JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
四、错误一解决方法:
设置Feature.ALLOW_SINGLE_QUOTES为true,表示允许单引号
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
五、错误二:
在标准json中,要求键和值都要用双引号("")包括的,而淘宝返回的健没有用引号,所以报错。
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('m' (code 109)): was expecting double-quote to start field name
六、错误二解决方法:
设置Feature.ALLOW_UNQUOTED_FIELD_NAMES为true,表示允许无引号包括的字段
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true); objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
七、完整代码
/** * 淘宝手机归属地查询api * @param phone * @return * @throws Exception * @throws IOException */ @ResponseBody @RequestMapping("/getPhoneMessage") public String getPhoneMessage(@RequestParam(value="phone") String phone) throws Exception, IOException{ //手机归属地查询api //淘宝,返回json //https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13430878244 //拍拍,返回json //http://virtual.paipai.com/extinfo/GetMobileProductInfo?mobile=13430878244&amount=10000&callname=getPhoneNumInfoExtCallback //百度钱包,返回json的unicode //https://www.baifubao.com/callback?cmd=1059&callback=phone&phone=13430878244 URI uri = new URIBuilder().setScheme("https").setHost("tcc.taobao.com") .setPath("/cc/json/mobile_tel_segment.htm") .setParameter("tel", phone).build(); CloseableHttpClient closeableHttpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(uri); CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpGet); HttpEntity httpEntity = closeableHttpResponse.getEntity(); InputStream inputStream = httpEntity.getContent(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"GBK")); StringBuffer stringBuffer = new StringBuffer(); String text = null; while((text =bufferedReader.readLine()) != null){ stringBuffer.append(text); } inputStream.close(); closeableHttpResponse.close(); String jsonString = stringBuffer.toString().split("=")[1].trim();//处理=号前的非json字符串 ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);//设置可用单引号 objectMapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);//设置字段可以不用双引号包括 JsonNode root = objectMapper.readTree(jsonString); return root.path("catName").asText() + root.path("carrier").asText(); }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
蕃薯耀 2015年9月19日 14:50:37 星期六
http://fanshuyao.iteye.com/