我是 JSON、WebDevelopment、Javascript 新手...
我有以下 JSON 文件
{
"component": "A",
"status": 0,
"children": [
{
"component": "AA",
"status": 0,
"children": [
{
"component": "AAA",
"status": 0,
"children": []
},
{
"component": "AAB",
"status": 0,
"children": []
}
]
},
{
"component": "AB",
"status": 0,
"children": [
{
"component": "ABA",
"status": 0,
"children": []
},
{
"component": "ABB",
"status": 0,
"children": []
}
]
}
]
}
我需要使用 Javascript/Jquery 读取它,然后显示它。稍后我应该开发代码,用于在网页上单击“组件 A”及其下的组件列表 应该显示等等。根据状态变量,颜色将被分配给显示的组件(以图像的形式,如盒子或其他东西)
我编写了以下代码:
<html>
<head>
<title>Demo</title>
</head>
<body>
<script src="jquery-1.9.1.min.js"></script>
<script src="jquery-1.9.1.js"></script>
<script>
$(document).ready(function() {
var myItems;
$.getJSON('jsonfile.json', function(data) {
JSONArray children = jsonfile.getJSONArray("children");
});
});
</script>
</body>
</html>
首先,JSON 的 java 函数的 javascript 等价物是什么,例如 getJSONArray()、getJSONObject() 等 其次,关于如何使用 API 等打印详细信息的任何建议。
最佳答案
请显示此上下文中 $.each 的语法
var result = $('#result');
$.each(obj.children, function(i, v){
result.append('<div>' + i + ' - ' + v.component + '</div>');
});
关于javascript - 使用 Javascript/Jquery 访问 JSON 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15743440/