溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

html中怎么美化展示json格式數(shù)據(jù)

發(fā)布時間:2021-04-26 11:56:31 來源:億速云 閱讀:626 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了html中怎么美化展示json格式數(shù)據(jù),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

html有什么特點

1、簡易性:超級文本標(biāo)記語言版本升級采用超集方式,從而更加靈活方便,適合初學(xué)前端開發(fā)者使用。 2、可擴展性:超級文本標(biāo)記語言的廣泛應(yīng)用帶來了加強功能,增加標(biāo)識符等要求,超級文本標(biāo)記語言采取子類元素的方式,為系統(tǒng)擴展帶來保證。  3、平臺無關(guān)性:超級文本標(biāo)記語言能夠在廣泛的平臺上使用,這也是萬維網(wǎng)盛行的一個原因。 4、通用性:HTML是網(wǎng)絡(luò)的通用語言,它允許網(wǎng)頁制作人建立文本與圖片相結(jié)合的復(fù)雜頁面,這些頁面可以被網(wǎng)上任何其他人瀏覽到,無論使用的是什么類型的電腦或瀏覽器。

直接上代碼:

html中主要加一個pre 的標(biāo)簽

<h3>GeoJsonTxt示例如下:</h3>
	<p>
	    <h5>注意:示例中<span style="color:red">features,geometry,coordinates,properties,name,description</span>字段是必須有的,
	        字段名稱不能改變,需要擴展的字段可以在<span style="color:red">properties</span>里面自行添加</h5>
	</p>
	<pre id="geoJsonTxt">
	</pre>

js代碼:

function showGeoJsonTxt(){
	$('#geoJsonTxt').html(JsonFormat());
}

function JsonFormat() {
	var json={
			  "type": "FeatureCollection",
			  "features": [
			    {
			      "type": "Feature",
			      "geometry": {
			        "type": "Point",
			        "coordinates": [
			          123,
			          22,
			          0
			        ]
			      },
			      "properties": {
			        "name": "必須有的字段",
			        "description": "必須有的字段",
					"age":"擴展字段,可以自行添加,擴展",
					"type":"擴展字段,可以自行添加,擴展"
			      }
			    },
				{
			      "type": "Feature",
			      "geometry": {
			        "type": "Point",
			        "coordinates": [
			          123,
			          32,
			          0
			        ]
			      },
			      "properties": {
			        "name": "必須有的字段",
			        "description": "必須有的字段",
			      }
			    }
			  ]
			};
	if (typeof json != 'string') {
		json = JSON.stringify(json, undefined, 2);
	}
		json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
		return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
			 var cls = 'number';
			 	if (/^"/.test(match)) {
			 			if (/:$/.test(match)) {
			 				cls = 'key';
			 			} else {
			 				cls = 'string';
			 			}
			 	} else if (/true|false/.test(match)) {
			 		cls = 'boolean';
			 	} else if (/null/.test(match)) {
			 		cls = 'null';
			 	}
			 	return '<span class="' + cls + '">' + match + '</span>';
		 });
}

CSS代碼,顯示的時候可以更加美化

<style>
 pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; }
 .string { color: green; }
 .number { color: darkorange; }
 .boolean { color: blue; }
 .null { color: magenta; }
 .key { color: red; }
</style>

效果如下:

html中怎么美化展示json格式數(shù)據(jù)

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“html中怎么美化展示json格式數(shù)據(jù)”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI