JsonValue 封裝類

執筆記憶的空白發表於2016-01-05

此文章是針對之前的一篇部落格做的一個補充


Json與Map、List、Object互轉



package com.jf.rentcar.util;

import java.util.HashMap;
import java.util.List;


public class JsonValue {
	private String value;
	private HashMap<String,JsonValue> map;
	private List<HashMap<String,JsonValue>> list;
	private Boolean bool;
	private Number number;

	public void setNumber(Number number) {
		this.number = number;
	}

	public Number getNumber() {
		return number;
	}

	public JsonValue(){
	}

	public JsonValue(String value){
		setValue(value);
	}
	
	public JsonValue(Integer value){
		setValue(StringUtil.valueOf(value));
	}
	public JsonValue(Double value){
		setValue(StringUtil.valueOf(value));
	}
	
	public JsonValue(Long value){
		setValue(String.valueOf(value));
	}
	
	public JsonValue(Boolean bool){
		setBool(bool);
	}

	public JsonValue(HashMap<String, JsonValue>  map){
		setMap(map);
	}

	public JsonValue(List<HashMap<String, JsonValue>> list){
		setList(list);
	}

	public String getValue() {
		return value;
	}
	
	public void setValue(String value) {
		this.value = value;
	}

	public HashMap<String, JsonValue> getMap() {
		return map;
	}
	
	public void setMap(HashMap<String, JsonValue> map) {
		this.map = map;
	}

	public List<HashMap<String, JsonValue>> getList() {
		return list;
	}
	
	public void setList(List<HashMap<String, JsonValue>> list) {
		this.list = list;
	}
	
	public Boolean getBool() {
		return bool;
	}

	public void setBool(Boolean bool) {
		this.bool = bool;
	}

	public Class getValueType(){
		if(getValue() != null){
			return getValue().getClass();
		}
		if(getMap() != null){
			return getMap().getClass();
		}
		if(getList() != null){
			return getList().getClass();
		}
		if(getBool() != null){
			return getBool().getClass();
		}
		if(getNumber() != null){
			return getNumber().getClass();
		}
		
		return null;
	}

	public Object getJsonValue(){
		if(getValue() != null){
			return getValue();
		}
		if(getMap() != null){
			return getMap();
		}
		if(getList() != null){
			return getList();
		}
		if(getBool() != null){
			return getBool();
		}
		if(getNumber() != null){
			return getNumber();
		}
		return null;
	}

}




相關文章