Json reponse unexpectedly returns [object Object]
All I want is a simple output of a JSON file. Right now it is giving me back [object Object]
.
What开发者_运维技巧 am I doing wrong?
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.getJSON("team.json",function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
The result
is apparently an object {}
, not an array []
as your code seems to expect. An object has several properties which you need to access individually.
Probably the JSON object in turn contains an array property which you need to access instead. E.g.
{
teams: [
// ...
]
}
which should then be accessed as follows:
$.each(result.teams, function(i, field) {
// ...
}
try
$(field + " ").appendTo("div");
精彩评论