如何在java脚本中打印此哈希表数据

How to print this hash Table data in java script

本文关键字:打印 哈希表 数据 脚本 java      更新时间:2023-09-26

如何在java脚本中打印此哈希表数据

var product = {
    "prduct_name": "Mobile",
    "product_attribute": {
        "attribute_name": "Brand",
        "type": "combo",
        "product_attribute_values": {
            "attribute_value": "Apple",
            "brand_price": "2000"
        }
    }
};

如果您的(或目标)浏览器有可用的 JSON 对象 ( Internet Explorer 8+、Firefox 3.1+、Safari 4+、Chrome 3+ 和 Opera 10.5+ 浏览器原生 JSON 支持(窗口。杰森))那么我建议作为快速开发解决方案。

str = JSON.stringify(product);

或者,如果您想要这一切,例如:

str = JSON.stringify(product, null, " ");

但是,这可能不适合面向客户端的显示器!

已编辑,因为未看到嵌套的对象数据。

http://jsfiddle.net/wYWQJ/

function printData(data) {
    var str = '';
    for (var key in data) {
        if (typeof data[key] == 'object') str += key + printData(data[key]) + ' ';
        else str += key + ' => ' + data[key] + ' ';
    }
    return str;
};
console.log(printData(product));