我得到一个包含可变数量对象的json文件,所有这些对象都不同。
以下是我的JSON文件示例。
{
"DNS": {
"Server": MYSERVER01
"IP": "XXX.XXX.XXX.XXX"
},
"TST": {
"SubKeyCount": 0,
"View": 0,
"Handle": {
"IsInvalid": false,
"IsClosed": false
},
"ValueCount": 309,
"Name": "HKEY_LOCAL_MACHINE\\Software\\TST",
"ComputerName": "MYSERVER01",
"Hive": -2147483646,
"Path": "Software\\TST"
},
"ServiceNow": null,
"InstalledSoftware": [
{
"Status": true,
"Software": [
"Symantec NetBackup 7.5",
"Symantec NetBackup Client",
],
"Version": [
"0000",
"7.5",
]
},
{
"Status": true,
"Software": "Symantec Endpoint Protection",
"Version": "12"
},
{
"Status": true,
"Software": "System Center 2012,
"Version": "7.0"
}
],
"AutoDuplex": {
"Description": "NIC Auto/Auto and Duplexing",
"Status": true
},
"DefaultAdGroups": [
{
"Result": true,
"Group": "Domain Admins"
},
{
"Result": true,
"Group": "Test-Team"
},
{
"Result": true,
"Group": MYSERVER01-ADMINS"
}
]
}
这只是少数可能在我的JSON文件中的对象。
目前我在jquery上创建一个ajax调用来读取文件并将json作为字符串发送给jquery。然后我将数据解析为json格式。
我对如何使用<ul>
整齐地显示此信息感到有点困惑,因为在文件中深深嵌入了如此多的数组。
我也不确定如何动态迭代json文件中的每个对象。在我开始之前,我只习惯每个文件看到1个对象。虽然这有点不同,但并不像指定:
那么容易var jsonQC = jQuery.parseJSON(result); //result from controller
jsonQC[1]
答案 0 :(得分:1)
有一些数据表jQuery插件可以显示复杂的JSon数据。以下是一个示例:https://editor.datatables.net/examples/advanced/deepObjects.html
或者,您可以使用递归函数,如下所述:jQuery JSON looping through nested objects
答案 1 :(得分:1)
如何使用dl / dt / dd?
function makeDom(obj) {
var $dl = $("<dl/>");
$.each(obj, function(name, val) {
$("<dt>").text(name).appendTo($dl);
var $dd = $("<dd>");
if(val && typeof val === "object") {
$dd.append(makeDom(val));
} else {
$dd.text(val);
}
$dd.appendTo($dl);
});
return $dl;
}
var obj = {
"DNS": {
"Server": "MYSERVER01",
"IP": "XXX.XXX.XXX.XXX"
},
"TST": {
"SubKeyCount": 0,
"View": 0,
"Handle": {
"IsInvalid": false,
"IsClosed": false
},
"ValueCount": 309,
"Name": "HKEY_LOCAL_MACHINE\\Software\\TST",
"ComputerName": "MYSERVER01",
"Hive": -2147483646,
"Path": "Software\\TST"
},
"ServiceNow": null,
"InstalledSoftware": [
{
"Status": true,
"Software": [
"Symantec NetBackup 7.5",
"Symantec NetBackup Client",
],
"Version": [
"0000",
"7.5",
]
},
{
"Status": true,
"Software": "Symantec Endpoint Protection",
"Version": "12"
},
{
"Status": true,
"Software": "System Center 2012",
"Version": "7.0"
}
],
"AutoDuplex": {
"Description": "NIC Auto/Auto and Duplexing",
"Status": true
},
"DefaultAdGroups": [
{
"Result": true,
"Group": "Domain Admins"
},
{
"Result": true,
"Group": "Test-Team"
},
{
"Result": true,
"Group": "MYSERVER01-ADMINS"
}
]
};
$(function() {
makeDom(obj).appendTo("body");
});