json formatter 一個檢查json格式是否正確的小工具
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Formatter and colorer of raw JSON code</title>
<meta name="description" content="A tool to format and color raw JSON code">
<meta name="keywords" content="Json, Printer, Colorer, Format, Color">
<script>
// we need tabs as spaces and not CSS magin-left
// in order to ratain format when coping and pasing the code
window.TAB = " ";
function IsArray(obj) {
return obj &&
typeof obj === 'object' &&
typeof obj.length === 'number' &&
!(obj.propertyIsEnumerable('length'));
}
function Process(){
var json = document.getElementById("RawJson").value;
var html = "";
try{
if(json == "") json = "/"/"";
var obj = eval("["+json+"]");
html = ProcessObject(obj[0], 0, false, false, false);
document.getElementById("Canvas").innerHTML = "<PRE class='CodeContainer'>"+html+"</PRE>";
}catch(e){
alert("JSON is not well formated:/n"+e.message);
document.getElementById("Canvas").innerHTML = "";
}
}
function ProcessObject(obj, indent, addComma, isArray, isPropertyContent){
var html = "";
var comma = (addComma) ? "<span class='Comma'>,</span> " : "";
var type = typeof obj;
if(IsArray(obj)){
if(obj.length == 0){
html += GetRow(indent, "<span class='ArrayBrace'>[ ]</span>"+comma, isPropertyContent);
}else{
html += GetRow(indent, "<span class='ArrayBrace'>[</span>", isPropertyContent);
for(var i = 0; i < obj.length; i++){
html += ProcessObject(obj[i], indent + 1, i < (obj.length - 1), true, false);
}
html += GetRow(indent, "<span class='ArrayBrace'>]</span>"+comma);
}
}else if(type == 'object' && obj == null){
html += FormatLiteral("null", "", comma, indent, isArray, "Null");
}else if(type == 'object'){
var numProps = 0;
for(var prop in obj) numProps++;
if(numProps == 0){
html += GetRow(indent, "<span class='ObjectBrace'>{ }</span>"+comma, isPropertyContent);
}else{
html += GetRow(indent, "<span class='ObjectBrace'>{</span>", isPropertyContent);
var j = 0;
for(var prop in obj){
html += GetRow(indent + 1, "<span class='PropertyName'>"+prop+"</span>: "+ProcessObject(obj[prop], indent + 1, ++j < numProps, false, true));
}
html += GetRow(indent, "<span class='ObjectBrace'>}</span>"+comma);
}
}else if(type == 'number'){
html += FormatLiteral(obj, "", comma, indent, isArray, "Number");
}else if(type == 'boolean'){
html += FormatLiteral(obj, "", comma, indent, isArray, "Boolean");
}else if(type == 'function'){
obj = FormatFunction(indent, obj);
html += FormatLiteral(obj, "", comma, indent, isArray, "Function");
}else if(type == 'undefined'){
html += FormatLiteral("undefined", "", comma, indent, isArray, "Null");
}else{
html += FormatLiteral(obj, "/"", comma, indent, isArray, "String");
}
return html;
}
function FormatLiteral(literal, quote, comma, indent, isArray, style){
if(typeof literal == 'string')
literal = literal.split("<").join("<").split(">").join(">");
var str = "<span class='"+style+"'>"+quote+literal+quote+comma+"</span>";
if(isArray) str = GetRow(indent, str);
return str;
}
function FormatFunction(indent, obj){
var tabs = "";
for(var i = 0; i < indent; i++) tabs += window.TAB;
var funcStrArray = obj.toString().split("/n");
var str = "";
for(var i = 0; i < funcStrArray.length; i++){
str += ((i==0)?"":tabs) + funcStrArray[i] + "/n";
}
return str;
}
function GetRow(indent, data, isPropertyContent){
var tabs = "";
for(var i = 0; i < indent && !isPropertyContent; i++) tabs += window.TAB;
if(data != null && data.length > 0 && data.charAt(data.length-1) != "/n")
data = data+"/n";
return tabs+data;
}
</script>
<style>
.Canvas
{
font: 10pt Georgia;
background-color:#ECECEC;
color:#000000;
border:solid 1px #CECECE;
}
.ObjectBrace
{
color:#00AA00;
font-weight:bold;
}
.ArrayBrace
{
color:#0033FF;
font-weight:bold;
}
.PropertyName
{
color:#CC0000;
font-weight:bold;
}
.String
{
color:#007777;
}
.Number
{
color:#AA00AA;
}
.Boolean
{
color:#0000FF;
}
.Function
{
color:#AA6633;
text-decoration:italic;
}
.Null
{
color:#0000FF;
}
.Comma
{
color:#000000;
font-weight:bold;
}
PRE.CodeContainer{
margin-top:0px;
margin-bottom:0px;
}
</style>
</head>
<body>
<div style="float:right;font-size:11px;">Quick Json Formatter 1.0 Copyright (c) 2008 Vladimir Bodurov <a href="http://blog.bodurov.com">http://blog.bodurov.com</a></div>
<h3 style="margin-bottom:2px">Formatter and colorer of raw JSON code</h3>
<div>Enter your JSON here:</div>
<textarea id="RawJson" cols="100" rows="8">
</textarea><BR/>
<input type="Button" value="Format" onClick="Process()"/>
<div id="Canvas" class="Canvas"></div>
</body>
</html>
給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow
相關文章
- 使用Json.NET驗證json字串是否格式正確JSON字串
- javascript中如何判斷一個字串是否為JSON格式JavaScript字串JSON
- js檢查身份證號是否正確JS
- npm-package-json-lint 是一個用於檢查 package.json 檔案格式和內容規範的 npm 包。NPMPackageJSON
- JSON格式JSON
- JSON 格式JSON
- 請寫一個函式驗證電子郵件的格式是否正確(要求使用正則)函式
- Python庫檢查是否正確安裝並安裝所需版本Python
- 是否存在JSON物件JSON物件
- js實現【JSON格式化】——方式1:正則JSON
- 08JSON格式JSON
- 寫一個方法檢測頁面中的所有標籤是否正確閉合
- android解析HashMap格式的jsonAndroidHashMapJSON
- 寫了一個json小工具,希望大家體驗(Mac平臺)JSONMac
- Json 查詢JSON
- python json格式轉url引數&分割, url引數轉json格式PythonJSON
- appium 如何檢測頁面文案是否正確APP
- Excel表格轉Json格式ExcelJSON
- 處理json格式的資料JSON
- 格式化 curl 返回的 jsonJSON
- 如何檢查一個物件是否為空物件
- 一個複雜的json例子JSON
- 使用.format格式化json格式的入參ORMJSON
- Python字典格式與JSON格式的相互轉換PythonJSON
- go fiber: controller返回json格式GoControllerJSON
- python讀取json格式的標註PythonJSON
- Python將xml格式轉換為json格式PythonXMLJSON
- 開發工具-scala處理json格式利器-json4sJSON
- 【elasticsearch】bulk api奇特的json格式的原因ElasticsearchAPIJSON
- Gradio/Streamlit 格式化 json(qbit)JSON
- js實現字串轉JSON格式字串JSON
- python 從TXT中解析json格式PythonJSON
- JSONObject與JSONArray 解析JSON格式字串JSONObject字串
- java json格式化的兩種方式JavaJSON
- 推薦一個Json解析庫JSON
- `newtonsoft.json` 指定某個屬性使用特定的時間格式JSON
- php檢查某個日期是否有效PHP
- iOS 中使用正規表示式判斷身份證格式及銀行卡號格式是否正確(推薦)iOS