json字符串轉換爲html字符串

json數據格式用於數據傳輸轉換是十分方便的,可是直接預覽的可讀性差,因此把json串轉換爲html串,能夠直接在頁面展現。
html

這種html展現在json列表中效果尤其明顯。java

注意:json格式必定要正確!  文件再大也不能分頁!json

public class Json2Html {

	private static String html = "";
	
	/**
	 * 將json格式字符串轉換成html字符串
	 * @param json String
	 * @return html String
	 */
	public static String jsonToHtml(String json) {
		//判斷json格式是否規範
		if (isGoodJson(json)) {
			JsonElement j = new JsonParser().parse(json);
			html = "";
			json2html(j);
			return html;
		} else {
			return "json數據格式不規範,沒法解析。</br>"+json;
		}
	}
	
	/**
	 * 判斷json串格式是否規範
	 * @param json String
	 * @return true(規範) false(不規範)
	 */
	public static boolean isGoodJson(String json) {
		if (StringUtils.isBlank(json)) {
			return false;
		}
		try {
			new JsonParser().parse(json);
			return true;
		} catch (JsonParseException e) {
//			System.out.println("bad json: " + json);
			return false;
		}
	}
	
	/**
	 * json轉html (遞歸)
	 * @param json gson對象
	 * 轉換過程不斷修改全局html String
	 */
	public static void json2html(JsonElement json){
		//數組  繪製表格
		if(json.isJsonArray()){
			JsonArray jArray = json.getAsJsonArray();
			Iterator it = jArray.iterator();  
			html += "<table class='tableList'>";
			int f = 0;
			while(it.hasNext()){  
			   JsonElement jsonElement=(JsonElement) it.next();
			   if(f == 0){
				   html += "<thead>";
				   jsonGetHead(jsonElement);
				   html += "</thead><tbody>";
			   }
			   html += "<tr>";
			   jsonGetBody(jsonElement);
			   html += "</tr>";
			   f++;
			}
			html += "</tbody>";
			html += "</table>";
		}else 
			//對象 (map)
			if(json.isJsonObject()){
			JsonObject jObject = json.getAsJsonObject();
		    Set<Entry<String, JsonElement>> entrySet = jObject.entrySet();
		    Iterator<Entry<String, JsonElement>> iter = entrySet.iterator();
		    while(iter.hasNext()){
//		    	htmlBegin += "<td>";
//		    	htmlEnd = "</td>" + htmlEnd;
		    	Entry<String, JsonElement> entry = iter.next();
		    	String key = entry.getKey();
		    	html += key;
		    	html += "=";
		    	JsonElement value = entry.getValue();
		    	json2html(value);
		    }	
		}else
			//單一字符
			if(json.isJsonPrimitive()){
			String finals = json.getAsString(); 
			html += finals;
		}else if(json.isJsonNull()){
		}
	}
	
	/**
	 * 數組繪製表格  添加表頭
	 * @param json
	 */
	private static void jsonGetHead(JsonElement json){
		JsonObject jObject = json.getAsJsonObject();
	    Set<Entry<String, JsonElement>> entrySet = jObject.entrySet();
	    Iterator<Entry<String, JsonElement>> iter = entrySet.iterator();
	    while(iter.hasNext()){
	    	Entry<String, JsonElement> entry = iter.next();
	    	String key = entry.getKey();
	    	html += "<td>" + key + "</td>";
	    }
	}
	
	/**
	 * 數組繪製表格  添加表體
	 * @param json
	 */
	private static void jsonGetBody(JsonElement json){
		JsonObject jObject = json.getAsJsonObject();
	    Set<Entry<String, JsonElement>> entrySet = jObject.entrySet();
	    Iterator<Entry<String, JsonElement>> iter = entrySet.iterator();
	    while(iter.hasNext()){
	    	Entry<String, JsonElement> entry = iter.next();
	    	html += "<td>";
	    	JsonElement value = entry.getValue();
	    	json2html(value);
	    	html += "</td>";
	    }
	}
}

遺憾的是,文件達到20M左右程序就基本癱瘓了。如何解?

數組

相關文章
相關標籤/搜索