微软云HowOld在线人脸检测年龄性别结果的json解析

2015年05月11日 其他资源 2条评论 阅读1951次

微软云HowOld在线人脸检测年龄性别结果的json解析

微软云HowOld在线人脸检测年龄性别的网站瞬间火边全球,因为该网站下面对输入图片的解析结果有提供json,

所以,就把改json解析了一下,方便后续做微软云HowOld在线人脸检测年龄性别的APP客户端应用

首先是一个结果bean,因为微软云HowOld在线人脸检测年龄性别返回的结果还是挺多的,所以就单独做了一个类。

package net.jileniao.howold;

import android.location.Location;

/**
 * 
 * @author Jileniao.Net
 *
 */
public class HowOldResultBean {

	private float age;
	private String gender;
	private String eventDatetime;
	private String userId;
	private String sessionId;
	private String submissionMethod;
	private String userAgent;
	private Location location;
	private Location locationCity;
	private boolean isMobileDevice;
	private String browserType;
	private String platform;
	private String mobileDeviceModel;

	public float getAge() {
		return age;
	}

	public void setAge(float age) {
		this.age = age;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getEventDatetime() {
		return eventDatetime;
	}

	public void setEventDatetime(String eventDatetime) {
		this.eventDatetime = eventDatetime;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getSubmissionMethod() {
		return submissionMethod;
	}

	public void setSubmissionMethod(String submissionMethod) {
		this.submissionMethod = submissionMethod;
	}

	public String getUserAgent() {
		return userAgent;
	}

	public void setUserAgent(String userAgent) {
		this.userAgent = userAgent;
	}

	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	public Location getLocationCity() {
		return locationCity;
	}

	public void setLocationCity(Location locationCity) {
		this.locationCity = locationCity;
	}

	public boolean isMobileDevice() {
		return isMobileDevice;
	}

	public void setMobileDevice(boolean isMobileDevice) {
		this.isMobileDevice = isMobileDevice;
	}

	public String getBrowserType() {
		return browserType;
	}

	public void setBrowserType(String browserType) {
		this.browserType = browserType;
	}

	public String getPlatform() {
		return platform;
	}

	public void setPlatform(String platform) {
		this.platform = platform;
	}

	public String getMobileDeviceModel() {
		return mobileDeviceModel;
	}

	public void setMobileDeviceModel(String mobileDeviceModel) {
		this.mobileDeviceModel = mobileDeviceModel;
	}
}

接下来就是重要的解析类了。

package net.jileniao.howold;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.location.Location;

/**
 * 
 * @author Jileniao.Net
 *
 */
public class HowOldJsonHelper {

	private List<HowOldResultBean> resultList;

	public HowOldJsonHelper() {
		resultList = new ArrayList<HowOldResultBean>();
	}

	public int getFaceCount() {
		return resultList.size();
	}

	public float getFaceAgeById(int index) {
		return resultList.get(index).getAge();
	}

	public String getFaceGenderById(int index) {
		return resultList.get(index).getGender();
	}
	
	public String getEventDataTimeById(int index) {
		return resultList.get(index).getEventDatetime();
	}
	
	public String getUserIdById(int index) {
		return resultList.get(index).getUserId();
	}

	public void parseHowOldResultJson(String howOldResultJson) throws JSONException {
		JSONArray array = new JSONArray(howOldResultJson);
		JSONObject obj;
		for (int i = 0; i < array.length(); i++) {
			obj = (JSONObject) array.get(i);
			HowOldResultBean bean = new HowOldResultBean();
			bean.setAge(Float.parseFloat(obj.getJSONObject("face").getString(
					"age")));
			bean.setGender(obj.getJSONObject("face").getString("gender"));
			bean.setEventDatetime(obj.getString("event_datetime"));
			bean.setUserId(obj.getString("user_id"));
			bean.setSessionId(obj.getString("session_id"));
			bean.setSubmissionMethod(obj.getString("submission_method"));
			bean.setUserAgent(obj.getString("user_agent"));
			Location location = new Location("");
			location.setLatitude(Double.parseDouble(obj.getJSONObject(
					"location").getString("latitude")));
			location.setLongitude(Double.parseDouble(obj.getJSONObject(
					"location").getString("longitude")));
			bean.setLocation(location);
			Location cityLocation = new Location("");
			cityLocation.setLatitude(Double.parseDouble(obj.getJSONObject(
					"location_city").getString("latitude")));
			cityLocation.setLongitude(Double.parseDouble(obj.getJSONObject(
					"location_city").getString("longitude")));
			bean.setLocationCity(cityLocation);

			bean.setMobileDevice(Boolean.parseBoolean(obj
					.getString("is_mobile_device")));
			bean.setBrowserType(obj.getString("browser_type"));
			bean.setPlatform(obj.getString("platform"));
			bean.setMobileDeviceModel(obj.getString("mobile_device_model"));

			resultList.add(bean);
		}
	}
}

构造函数之后,调用parseHowOldResultJson(String)即可解析。

得到结果调用响应的get方法就行了l

分享本文至:

WRITTEN BY

avatar
本文标签:json微软
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

2 条评论

  1. avatar 久闻网佳莉

    它是通过什么思维进行分析的

    • @久闻网佳莉:简单说来,主要是面部检测、性别分类和年龄检测三项技术的综合利用。
      具体说来,这三项技术中每一项技术都值得画上N年去研究。。。

欢迎留言




用户登录

sitemap