【问题标题】:.getjson from external domain. Print from the Array.getjson 来自外部域。从阵列打印
【发布时间】:2012-10-12 19:50:31
【问题描述】:

我目前从以下 get 语句中获取 2 个数组。

 $.getJSON("http://domain.com?callback=?", function(data){
  $("#results").html(JSON.stringify(data));

我将结果附加到带有 id 结果的 div 中。

所做的只是显示数组。我需要从数组中提取某些对象,但我不能这样做,因为我只是在打印整个数组。如何从该数组中获取特定信息?

这里是附加到 DIV 的一些数据。

{"me":[{"player":{"high_score":110345,"rank":2}}],"all":[{"player":{"@score := s.score" :110345,"头像":"http://profile.ak.fbcdn.net/hprofile-ak-snc6/195312_789328764_1121893995_q.jpg","guid":"FE4EC535-B74F-4B68-8F4D-2CA0EBC28FAF","名称" :"Charles Chase","rank":1,"score":110345}},

谢谢!

【问题讨论】:

  • 能把返回的json数据贴一下吗?
  • 我将其添加到问题中。谢谢!
  • 你确定吗,它看起来不完整?

标签: jsonp getjson


【解决方案1】:

前提是你返回的JSON数据如下

{    
    "me": [
                {
                    "player": {
                        "high_score": 110345,
                        "rank": 2
                    }
                }
            ],
            "all": [
                {
                    "player": {
                        "@score := s.score": 110345,
                        "avatar": "http://profile.ak.fbcdn.net/hprofile-ak-snc6/195312_789328764_1121893995_q.jpg",
                        "guid": "FE4EC535-B74F-4B68-8F4D-2CA0EBC28FAF",
                        "name": "Charles Chase",
                        "rank": 1,
                        "score": 110345
                    }
                }
            ]
        };

您可以使用以下方法从 JSON 对象中获取值

代码

$.getJSON("http://domain.com?callback=?", function(data){
  alert(data.me[0].player.high_score);
  alert(data.all[0].player.name);
));

注意meall 作为数组返回。希望对你有帮助。

【讨论】:

  • 太棒了。如果我想在页面上写输出,我会这样做: var test = (data.all[0].player.name); document.write(test);
  • 我想你已经有一个带有 id 结果的 div。就这么做 $("#results").html(data.all[0].player.name);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-21
  • 1970-01-01
  • 2019-05-08
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多