开发者

Json reponse unexpectedly returns [object Object]

开发者 https://www.devze.com 2023-02-21 18:30 出处:网络
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?

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消