一个可以随时插入的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的简单转换