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

前端之家收集整理的这篇文章主要介绍了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<Map<String,String>> ls = new LinkedList<Map<String,String>>();
	Map<String,String> object = null;
	
	public void newObject(){
		object = new LinkedHashMap<String,String>();
	}
	
	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<String,String> item){
//		ls.add(item);
//	}
	
	public void add(JSONArrayBuilder ja){
		add(ja.getJsonArray());
	}
	
	public void add(List<Map<String,String>> ls){
		this.ls.addAll(ls);
	}
	
	public List<Map<String,String>> getJsonArray(){
		return ls;
	}
	
	/**
	 * 将数据转换成json格式字符�?
	 * @return
	 */
	public String toJsonString(){
		String sb = "";
		String key = null;
		String value = null;
		Iterator<String> keys = null;
		
		sb  = "[";
		
		for(Map<String,String> 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","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);
		
	}
}
原文链接:https://www.f2er.com/json/290624.html

猜你在找的Json相关文章