一个可以随时插入的json的简单实现

package net.totosea.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 用Map来实现Json
 * 
 * @author huang
 * 
 */
public class ResultJsonRange implements Map<String, String> {

	Map<String, String> map = new HashMap<String, String>();

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
		this.stringBuffer = createStringBuffer(this.map);
	}

	private StringBuffer stringBuffer;

	public StringBuffer getStringBuffer() {
		return stringBuffer;
	}

	public void setStringBuffer(StringBuffer stringBuffer) {
		this.stringBuffer = stringBuffer;
		String str = this.stringBuffer.toString();
		Map<String, String> data = new HashMap<String, String>();
		String reg = "\".+?\":\".*?\"";
		Pattern p = Pattern.compile("^[{].*?[}]$");
		Matcher m = p.matcher(str);
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(str);
		if (m.find()) {
			while (matcher.find()) {
				String s = matcher.group();
				if (s.contains(":")) {
					String key = s.split(":")[0];
					String value = s.split(":")[1];
					data.put(key, value);
				}
			}
		}
		this.map = data;
	}

	@Override
	public void clear() {
		this.stringBuffer = null;
		this.map = null;
	}

	@Override
	public boolean containsKey(Object key) {
		if (map.containsKey(key)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean containsValue(Object value) {
		if (map.containsValue(value)) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Set<java.util.Map.Entry<String, String>> entrySet() {
		return map.entrySet();
	}

	@Override
	public String get(Object key) {
		return map.get(key);
	}

	@Override
	public boolean isEmpty() {
		if (map.size() > 0) {
			return false;
		} else {
			return true;
		}
	}

	@Override
	public Set<String> keySet() {
		return this.map.keySet();
	}

	@Override
	public String put(String key, String value) {
		if (key == null || key == "") {
			throw new NullPointerException("key为空!");
		} else {
			this.map.put(key, value);
			this.stringBuffer = createStringBuffer(this.map);
		}
		return key;
	}

	private StringBuffer createStringBuffer(Map<String, String> map) {
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		Set<String> keyset = this.map.keySet();
		for (Iterator it = (Iterator) keyset.iterator(); it.hasNext();) {
			String key = (String) it.next();
			sb.append("\"" + key + "\"");
			sb.append(":");
			sb.append("\"" + this.map.get(key) + "\"");
			sb.append(",");
		}
		sb.replace(0, sb.length(), sb.toString().substring(0,
				sb.toString().length() - 1));
		sb.append("}");
		return sb;
	}

	@Override
	public void putAll(Map<? extends String, ? extends String> m) {
		// TODO Auto-generated method stub
	}

	@Override
	public String remove(Object key) {
		if (containsKey(key)) {
			String value = map.get(key);
			map.remove(key);
			this.stringBuffer = createStringBuffer(this.map);
			return value;
		} else {
			return null;
		}

	}

	@Override
	public int size() {
		return this.map.size();
	}

	@Override
	public Collection<String> values() {
		return this.map.values();
	}

}

 测试代码:

	public static void main(String[] s) {
		ResultJsonRange r = new ResultJsonRange();
		r.put("flag", "true");
		r.put("context", "sss");
		StringBuffer sb = r.getStringBuffer();
		System.out.print(sb.toString());
		System.out.println("0000000");
		System.out.println("0000000");
	}

 测试结果:

{"flag":"true","context":"sss"}0000000
0000000

 这种实现目前只支持简单的单层json格式,对map的简单转换

转载于:https://www.cnblogs.com/tatame/archive/2012/03/30/2425737.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值