JSONArrayBuilder:用于生成格式简单的json array字符串


JSONArrayBuilder

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 用于生成json数组
 * @author shanl
 *
 */
public class JSONArrayBuilder {
	List> ls = new LinkedList>();
	Map object = null;
	
	public void newObject(){
		object = new LinkedHashMap();
	}
	
	public void endObject(){
		if(null==object){
			throw new RuntimeException("请先调用newObject()");
		}
		
		ls.add(object);		
	}
	
	public void put(String key, String value){
		if(null==object){
			throw new RuntimeException("请先调用newObject()");
		}
		
		this.object.put(replaceJsonChar(key), replaceJsonChar(value));
	}
	
//	public void add(Map item){
//		ls.add(item);
//	}
	
	public void add(JSONArrayBuilder ja){
		add(ja.getJsonArray());
	}
	
	public void add(List> ls){
		this.ls.addAll(ls);
	}
	
	public List> getJsonArray(){
		return ls;
	}
	
	/**
	 * 将数据转换成json格式字符�?
	 * @return
	 */
	public String toJsonString(){
		String sb = "";
		String key = null;
		String value = null;
		Iterator keys = null;
		
		sb  = "[";
		
		for(Map item: ls){
			keys = item.keySet().iterator();
			sb += "{";
			
			while(keys.hasNext()){
				key = keys.next();
				value = item.get(key);
				
				sb += "\""+ key +"\":";
				sb += "\""+ value +"\",";
			}
			
			if(sb.endsWith(",")){
				sb = sb.substring(0, sb.length()-1);
			}			
			sb += "},";
		}
				
		if(sb.endsWith(",") ){
			sb = sb.substring(0, sb.length()-1);
		}
		
		sb += "]";
		
		sb = sb.replaceAll("\\{\\},", "");
		sb = sb.replaceAll("\\{\\}", "");
		
		return sb;
	}
	
	public String toString(){
		return toJsonString();
	}
	
	private String replaceJsonChar(String str){
		StringBuilder sb = new StringBuilder();		
		char[] chs = str.toCharArray();
		
		for(char c: chs){
			switch (c) { 
	        case '\"': 
	            sb.append("\\\""); 
	            break; 
	        case '\\': 
	            sb.append("\\\\"); 
	            break; 
	        case '/': 
	            sb.append("\\/"); 
	            break; 
	        case '\b': 
	            sb.append("\\b"); 
	            break; 
	        case '\f': 
	            sb.append("\\f"); 
	            break; 
	        case '\n': 
	            sb.append("\\n"); 
	            break; 
	        case '\r': 
	            sb.append("\\r"); 
	            break; 
	        case '\t': 
	            sb.append("\\t"); 
	            break; 
	        default: 
	            sb.append(c); 
	        } 
		}
		
		return sb.toString();
	}
}



JSONArrayBuilder 的测试类

public class Test4 {
	public static void main(String[] args){
		t2();
//		t1();
	}
	
	static void t2(){
		JSONArrayBuilder jsonArrayBuilder = new JSONArrayBuilder();
		
		jsonArrayBuilder.newObject();
//		for(int i=0; i<2; i++){
//			jsonArrayBuilder.put("msg", "测试消息"+i);
//			jsonArrayBuilder.put("result", "true");
//		}
		jsonArrayBuilder.endObject();
		
		
		jsonArrayBuilder.newObject();
		for(int i=0; i<5; i++){
			jsonArrayBuilder.put("msg"+i, "测试消息"+i);
			jsonArrayBuilder.put("result", "true");
		}
		jsonArrayBuilder.endObject();
		
		String ja = jsonArrayBuilder.toJsonString();
		System.out.println(ja);
	}
	
	static void t1(){
		JSONArrayBuilder jsonArrayBuilder = new JSONArrayBuilder();
				
		for(int i=0; i<2; i++){
			jsonArrayBuilder.newObject();
			
			jsonArrayBuilder.put("msg", "测试消息"+i);
			jsonArrayBuilder.put("result", "true");
			
			jsonArrayBuilder.endObject();
		}
		
		JSONArrayBuilder jsonArrayBuilder0 = new JSONArrayBuilder();
		
		for(int i=0; i<2; i++){
			jsonArrayBuilder0.newObject();
			
			jsonArrayBuilder0.put("msg", "测试消息"+i);
			jsonArrayBuilder0.put("result", "true");
			
			jsonArrayBuilder0.endObject();
		}
		
		jsonArrayBuilder.add(jsonArrayBuilder0);
		String ja = jsonArrayBuilder.toJsonString();
		System.out.println(ja);
		
	}
}



你可能感兴趣的:(json,array,json)