如何将此JSON对象修复为html表代码?

时间:2013-05-08 14:48:14

标签: javascript html json recursion html-table

我一直在修改我发现的一段代码,但我不能让它按照我的意愿工作。这是当前的Javascript:

function JsonUtil() {
    /**
     * Given a provided object,
     * return a string representation of the object type.
     */
    this.isType = function (obj_) {
        if (obj_ === null) {
            return "null";
        }
        if (obj_ === NaN) {
            return "Nan";
        }
        var _type = typeof obj_;
        switch (_type) {
        case "undefined":
            return "undefined";
        case "number":
            return "number";
        case "boolean":
            return "boolean";
        case "string":
            return "string";
        case "function":
            return "function";
        case "object":
            if (this.isArray(obj_)) {
                return "array";
            }
            return "associative";
        }
    },
    /**
     * Recursively search and display array as an HTML table.
     */
    this.tableifyArray = function (array_) {
        if (array_.length === 0) {
            return "[ empty ]";
        }

        var _out = "<table class='arrayTable'>";  

        for(var i = 0; i < array_.length; i++) {
            _out += "<tr class='arrayTr'><td class='arrayTd'>"
                 + this.tableifyObject(array_[i]) + "</td></tr>";
        }
        _out += "</table>";
        return _out;
    },
    /**
     * Recursively search and display common javascript types as an HTML table.
     */
    this.tableifyObject = function (obj_) {
        /*
   if (obj_ === '') {
        return "[ empty ]";
     }
     */
        switch (this.isType(obj_)) {
        case "null":
            return "¡The data object is null!";
        case "undefined":
            return "undefined";
        case "number":
            return obj_;
        case "boolean":
            return obj_;
        case "string":
            return obj_;
        case "array":
            return this.tableifyArray(obj_);
        case "associative":
            return this.tableifyAssociative(obj_);
        }
        return "!error converting object!";
    },
    /**
     * Recursively search and display associative array as an HTML table.
     */
    this.tableifyAssociative = function (array_) {
        if (this.isEmpty(array_)) {
            return "{ empty }";
        }

        var _out = "<table class='associativeTable'>";

        for (var _index in array_) {
            _out += "<tr class='associativeTr'><td class='associativeTd'>"
                + this.tableifyObject(_index) + "</td><td class='associativeTd'>"
                + this.tableifyObject(array_[_index]) + "</td></tr>";
        }
        _out += "</table>";
        return _out;
    },
    /**
     * identify if an associative array is empty
     */
    this.isEmpty = function (map_) {
        for (var _key in map_) {
            if (map_.hasOwnProperty(_key)) {
                return false;
            }
        }
        return true;
    },
    /**
     * Identify is an array is a 'normal' (not associative) array
     */
    this.isArray = function (v_) {
        return Object.prototype.toString.call(v_) == "[object Array]";
    },
    /**
     * given the desire to populate a map of maps, adds a master key,
     * and child key and value to a provided object.
     */
    this.addToMapOfMaps = function (map_, mkey_, key_, value_) {
        if (map_ === undefined) {
            map_ = {};
        }
        if (map_[mkey_] === undefined) {
            map_[mkey_] = {}
        }
        if (map_[mkey_][key_] === undefined) {
            map_[mkey_][key_] = null;
        }
        map_[mkey_][key_] = value_;
        return map_;
    }
}

这是CSS:

 .arrayTable { border: 1px solid #c2c2c2; background-color: #c2c2c2; padding: 5px;}
 .arrayTr { border: 1px solid #c2c2c2; background-color: #c2c2c2; padding: 5px;}
 .arrayTd { border: 1px solid #c2c2c2; background-color: #c2c2c2; padding: 5px; vertical-align: top;}
 .associativeTable { border: 1px solid #c2c2c2; background-color: #fff; padding: 5px;}
 .associativeTr { border: 1px solid #c2c2c2; background-color: #fff; padding: 5px;}
 .associativeTd { border: 1px solid #c2c2c2; background-color: #eee; padding: 5px; vertical-align: top;}

打电话给你只是做:

var json = new JsonUtil();
json.tableifyObject(jsonObject);

工作真的很棒,但不是我想要的,目前数组表显示如下:

NAME 123123
ID 1
CATEGORY 12412

NAME AAAA
ID 2
CATEGORY 2123

我想修改数组表的显示方式,垂直地需要它们以及一个表结构中的所有数据,而不是每个寄存器中的很多表。像这样:

NAME ID CATEGORY
123123 1 12412
AAAA 2 2123

如何更改递归Javascript以创建此类结果?

1 个答案:

答案 0 :(得分:0)

做到了!如果_obj是一个数组,则添加代码以生成另一种类型的表。 万一有人需要同样的东西,这很有效!

function createTableBasedOnJsonArray(array_) {

    var _out = "";      
    for(var index in array_) {
        if(index == 0) {
            _out += "<table class='arrayTable'><thead>";
            //create thead
            for(var key in array_[0]) {
                _out += "<th>" + key + "</th>";
            }
            _out += "</thead>";
        }
        var values = array_[index];     
        //create tbody
        _out += "<tbody><tr class='arrayTr'>";
        for(var key in values) {
            _out += "<td class='arrayTd'>" + values[key] + "</td>";
        }
        _out += "</tr>";
    }
    _out += "</tbody></table>"
    return _out;
}

因此,JSON Object to HTML的整个json库现在如下:

function JsonUtil() {
    /**
     * Given a provided object,
     * return a string representation of the object type.
     */
    this.isType = function (obj_) {
        if (obj_ === null) {
            return "null";
        }
        var _type = typeof obj_;
        switch (_type) {
        case "undefined":
            return "undefined";
        case "number":
            return "number";
        case "boolean":
            return "boolean";
        case "string":
            return "string";
        case "function":
            return "function";
        case "object":
            if (this.isArray(obj_)) {
                return "array";
            }
            return "associative";
        }
    },
    /**
     * Recursively search and display array as an HTML table.
     */
    this.tableifyArray = function (array_) {
        if (array_.length === 0) {
            return "[ empty ]";
        }

        var _out = createTableBasedOnJsonArray(array_);

        return _out;
    },
    /**
     * Recursively search and display common javascript types as an HTML table.
     */
    this.tableifyObject = function (obj_) {
        switch (this.isType(obj_)) {
        case "null":
            return "¡The data object is null!";
        case "undefined":
            return "undefined";
        case "number":
            return obj_;
        case "boolean":
            return obj_;
        case "string":
            return obj_;
        case "array":
            return this.tableifyArray(obj_);
        case "associative":
            return this.tableifyAssociative(obj_);
        }
        return "!error converting object!";
    },
    /**
     * Recursively search and display associative array as an HTML table.
     */
    this.tableifyAssociative = function (array_) {
        if (this.isEmpty(array_)) {
            return "{ empty }";
        }

        var _out = "<table class='associativeTable'>";

        for (var _index in array_) {
            _out += "<tr class='associativeTr'><td class='associativeTd'>"
                + this.tableifyObject(_index) + "</td><td class='associativeTd'>"
                + this.tableifyObject(array_[_index]) + "</td></tr>";
        }
        _out += "</table>";
        return _out;
    },
    /**
     * identify if an associative array is empty
     */
    this.isEmpty = function (map_) {
        for (var _key in map_) {
            if (map_.hasOwnProperty(_key)) {
                return false;
            }
        }
        return true;
    },
    /**
     * Identify is an array is a 'normal' (not associative) array
     */
    this.isArray = function (v_) {
        return Object.prototype.toString.call(v_) == "[object Array]";
    },
    /**
     * given the desire to populate a map of maps, adds a master key,
     * and child key and value to a provided object.
     */
    this.addToMapOfMaps = function (map_, mkey_, key_, value_) {
        if (map_ === undefined) {
            map_ = {};
        }
        if (map_[mkey_] === undefined) {
            map_[mkey_] = {}
        }
        if (map_[mkey_][key_] === undefined) {
            map_[mkey_][key_] = null;
        }
        map_[mkey_][key_] = value_;
        return map_;
    }
}

function createTableBasedOnJsonArray(array_) {

    var _out = "";      
    for(var index in array_) {
        if(index == 0) {
            _out += "<table class='arrayTable'><thead>";
            //create thead
            for(var key in array_[0]) {
                _out += "<th>" + spaceRawKeyName(key).toUpperCase() + "</th>";
            }
            _out += "</thead>";
        }
        var values = array_[index];     
        //create tbody
        _out += "<tbody><tr class='arrayTr'>";
        for(var key in values) {
            _out += "<td class='arrayTd'>" + values[key] + "</td>";
        }
        _out += "</tr>";
    }
    _out += "</tbody></table>"
    return _out;
}

如果有人对“spaceRawKeyName”函数感兴趣,该函数用于通过大写锁定识别将此accountId的字符串分隔为account_Id。

function spaceRawKeyName(string) {  
    var newone = [];
    for(i=0; i<string.length;i++) {
        character = string.charAt(i);
        if(character == character.toUpperCase()) {
            newone.push("_");
        }
        newone.push(character);     
    }
    var text = "";
    for(i=0;i < newone.length;i++){
        text += newone[i];
    }
    return text;
} 

当然你可以稍后制作一个.toUpperCase()让它看起来更加花哨ACCOUNT_ID,顺便说一句...如果你不想添加“_”,你可以简单地替换一个空格。