Jackson String 转 Json 数据注意事项

原创
2017/03/22 09:23
阅读数 1.3W

框架:Jackson

先贴个数据接口传递过来的数据,guideMachineLocations后面的集合都是一样的,火狐格式化json数据插件:JSON Lite。

先判断数据类型,是单个对象,多个对象还是单个对象内嵌多个对象集合,对应要采用不同的方法进行接收和处理,否则会出错。

这个明显是单个对象,内嵌多个对象集合,可采用转换为实体对象的方式处理(我这里的示例是转化为实体对象,还有其他一堆方法自行度娘):

代码1-测试类:

package com.thinkgem.jeesite.common.test;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.utils.StreamUtils;

public class SkylineHttpAcceptTest {

	public static void main(String[] args) throws Exception {
	     ObjectMapper objectMapper = new ObjectMapper();
	     String urlStr = "链接";
	     URL url = new URL(urlStr);
             URLConnection conn = url.openConnection();
             conn.connect();
             InputStream is = conn.getInputStream();
             String inStr = StreamUtils.InputStreamTOString(is);
             System.out.println(inStr);
             is.close();
        
             HotPoint acc = objectMapper.readValue(inStr, HotPoint.class);
             System.out.println(acc); 
	}
}

代码2-对象类:

package com.thinkgem.jeesite.common.test;

import java.util.List;

public class HotPoint {

	private int operationOrgId;
	private String operationOrgName;
	private String centerLongitude;
	private String centerLatitude;
	private int rangeRadius;
	private List<GuideMachineLocationsBean> guideMachineLocations;

	public int getOperationOrgId() {
		return operationOrgId;
	}

	public void setOperationOrgId(int operationOrgId) {
		this.operationOrgId = operationOrgId;
	}

	public String getOperationOrgName() {
		return operationOrgName;
	}

	public void setOperationOrgName(String operationOrgName) {
		this.operationOrgName = operationOrgName;
	}

	public String getCenterLongitude() {
		return centerLongitude;
	}

	public void setCenterLongitude(String centerLongitude) {
		this.centerLongitude = centerLongitude;
	}

	public String getCenterLatitude() {
		return centerLatitude;
	}

	public void setCenterLatitude(String centerLatitude) {
		this.centerLatitude = centerLatitude;
	}

	public int getRangeRadius() {
		return rangeRadius;
	}

	public void setRangeRadius(int rangeRadius) {
		this.rangeRadius = rangeRadius;
	}

	public List<GuideMachineLocationsBean> getGuideMachineLocations() {
		return guideMachineLocations;
	}

	public void setGuideMachineLocations(
			List<GuideMachineLocationsBean> guideMachineLocations) {
		this.guideMachineLocations = guideMachineLocations;
	}

	public static class GuideMachineLocationsBean {

		private String _id;
		private String did;
		private String udate;
		private String ip;
		private int status;
		private LocBean loc;

		public String get_id() {
			return _id;
		}

		public void set_id(String _id) {
			this._id = _id;
		}

		public String getDid() {
			return did;
		}

		public void setDid(String did) {
			this.did = did;
		}

		public String getUdate() {
			return udate;
		}

		public void setUdate(String udate) {
			this.udate = udate;
		}

		public String getIp() {
			return ip;
		}

		public void setIp(String ip) {
			this.ip = ip;
		}

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

		public LocBean getLoc() {
			return loc;
		}

		public void setLoc(LocBean loc) {
			this.loc = loc;
		}

		public static class LocBean {

			private double longitude;
			private double latitude;

			public double getLongitude() {
				return longitude;
			}

			public void setLongitude(double longitude) {
				this.longitude = longitude;
			}

			public double getLatitude() {
				return latitude;
			}

			public void setLatitude(double latitude) {
				this.latitude = latitude;
			}
		}
	}
}

常见问题:

一般为判断数据格式错误,把单个对象混淆为多个对象,使用了错误的转换方法。

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部