json字符串转换为html字符串

json数据格式用于数据传输转换是十分方便的,但是直接预览的可读性差,所以把json串转换为html串,可以直接在页面展示。

这种html展示在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左右程序就基本瘫痪了。如何解?

转载于:https://my.oschina.net/u/2462382/blog/600884

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值