Java使用jackson问题解决
生活随笔
收集整理的這篇文章主要介紹了
Java使用jackson问题解决
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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=13430878244URI 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/
總結
以上是生活随笔為你收集整理的Java使用jackson问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 版图设计
- 下一篇: bt3 u盘版安装教程_bt3下载中文版