当前位置: 首页 > news >正文

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=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/

转载于:https://www.cnblogs.com/fanshuyao/p/6227168.html

相关文章:

  • resin+nginx搭建高性能Java服务器
  • Quartz2D 给图片打水印
  • 【转】FAE及其发展前景
  • SQL自动调优
  • design pattern scard
  • 第 十 二 天 : shell 入 门 和 简 单 的 命 令
  • vim 程序编辑器
  • 普通用户启动apache服务
  • 【转】浮点数与十六进制如何转换?
  • 串口硬流控原理验证RTS与CTS
  • 1、计科研究生的学习之路
  • mysql支持emoji表情存取的解决方案
  • Android编译错误, Ignoring InnerClasses attribute for an anonymous inner class
  • Android中的资源与国际化!
  • frame与bounds
  • [译]如何构建服务器端web组件,为何要构建?
  • ➹使用webpack配置多页面应用(MPA)
  • Android开源项目规范总结
  • Eureka 2.0 开源流产,真的对你影响很大吗?
  • Js基础知识(四) - js运行原理与机制
  • Js基础知识(一) - 变量
  • laravel with 查询列表限制条数
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • use Google search engine
  • vue脚手架vue-cli
  • 分享一份非常强势的Android面试题
  • 手写双向链表LinkedList的几个常用功能
  • 双管齐下,VMware的容器新战略
  • 最近的计划
  • kubernetes资源对象--ingress
  • #Linux(帮助手册)
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • %@ page import=%的用法
  • (3)STL算法之搜索
  • (done) NLP “bag-of-words“ 方法 (带有二元分类和多元分类两个例子)词袋模型、BoW
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (超简单)使用vuepress搭建自己的博客并部署到github pages上
  • (二)linux使用docker容器运行mysql
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (转)jdk与jre的区别
  • (转)shell调试方法
  • (转)拼包函数及网络封包的异常处理(含代码)
  • (转)树状数组
  • (转)项目管理杂谈-我所期望的新人
  • .Net Attribute详解(上)-Attribute本质以及一个简单示例
  • .net CHARTING图表控件下载地址
  • .net core Swagger 过滤部分Api
  • .NET 命令行参数包含应用程序路径吗?
  • .net 微服务 服务保护 自动重试 Polly
  • .net6 webapi log4net完整配置使用流程
  • .Net各种迷惑命名解释
  • .net解析传过来的xml_DOM4J解析XML文件
  • @EnableConfigurationProperties注解使用
  • @FeignClient注解,fallback和fallbackFactory