动态显示JSON文件内容 [英] Dynamically display JSON file contents
问题描述
我得到了一个json文件,其中包含可变数量的对象,所有这些对象都是不同的.
I am given a json file that contains a variable amount objects, all of which are different.
这是我的JSON文件的示例.
Here is an example of my JSON file.
{
"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文件中的少数几个对象.
This is just a handful of objects that could be in my JSON file.
目前,我正在通过我的jquery进行ajax调用,以读取文件并将jjson作为字符串发送给我的jquery.然后,我将数据解析为json格式.
Currently I am making an ajax call form my jquery to read the file and send my jquery the json as a string. I am then parsing the data into json format.
我对如何使用<ul>
整齐地显示此信息感到有些困惑,因为在文件内深处嵌套了许多数组.
I am a little confused on how I can display this information neatly using <ul>
's since there are so many arrays nested down deeply within the file.
我也不知道如何动态遍历json文件中的每个对象.在开始之前,我只习惯于每个文件看到1个对象.但是,这有些不同,并且不如指定容易:
I also am not sure how to dynamically iterate through each object in the json file. Before I started this, I was only used to seeing 1 object per file. This is a little different though and it is not as easy as specifying:
var jsonQC = jQuery.parseJSON(result); //result from controller
jsonQC[1]
推荐答案
如何使用dl/dt/dd?
How about using 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");
});
在这里拨弄: http://jsfiddle.net/robbyn/0u21ewon/
这篇关于动态显示JSON文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!