无法解析JSON数据

时间:2016-07-09 19:47:41

标签: jquery html json api

我无法正确解析应用程序中给定JSON端点的数据。我的代码正确地与服务器通信,但代码没有显示数据。我在JSFiddle上提供了我的代码链接:https://jsfiddle.net/SWTORApps/263d62ec/

我提供了一个示例输出,只是在没有响应的情况下。我正在使用的API端点目前正在部署一些更新,因此可能会在接下来的几天内不时脱机。列出的结果是针对搜索词:test

{
  "total_items": 2,
  "total_pages": 1,
  "current_page": 1,
  "sort_order": null,
  "items_per_pags": 30,
  "next_page": "http://swtordata.com/api/v2_2/items?page=2&query=test",
  "previous_page": null,
  "query": "test",
  "objects": [
    {
      "id": 8479,
      "stack_count": 0,
      "minimum_level": 3,
      "max_durability": 200,
      "value": 85,
      "display_name": "Battle-tested Vibrosword",
      "description": null,
      "giftrank": null,
      "imp_voice_modulation": null,
      "rep_voice_modulation": null,
      "armor_rating": 18,
      "consumed_on_use": false,
      "unique_limit": 0,
      "required_gender": null,
      "social_score_required": false,
      "social_score_required_rank": 0,
      "required_valor_rank": 0,
      "required_profession_level": null,
      "api_link": "http://swtordata.com/api/v2_2/items/8479",
      "api_icon": "http://cdn.swtordata.com/icons/electroblade.low05.a02_v01.png",
      "disassemble": "ArmstechBlasterRifle",
      "binding": "OnEquip",
      "website_link": "http://swtordata.com/items/8479-battle-tested-vibrosword",
      "item_level": 4,
      "armor_spec": null,
      "enhancement_type": null,
      "gift_type": null,
      "quality": {
        "name": "Custom",
        "darkdisplaycolor": "Orange",
        "lightdisplaycolor": "Orange",
        "darkdisplaycolor_hex": "d15901",
        "lightdisplaycolor_hex": "d15901"
      },
      "use_ability": null,
      "equip_ability": null,
      "category": "LightSaber",
      "subcategory": "VibroSword",
      "get_tooltip": {
        "onmouseout": "ajax_hideTooltip();",
        "onmouseover": "ajax_showTooltip(window.event,(\"http://swtordata.com/widgettext.html?url='http://swtordata.com/items/8479'\"),this);return false;"
      },
      "combined_stats": [
        {
          "stat_amount": 9,
          "item_stat_type": {
            "name": "STAT_att_endurance",
            "display_text": "Endurance"
          }
        },
        {
          "stat_amount": 6,
          "item_stat_type": {
            "name": "STAT_att_mastery",
            "display_text": "Mastery"
          }
        }
      ],
      "base_stats": [
        {
          "item_stat": {
            "stat_amount": 9,
            "item_stat_type": {
              "name": "STAT_att_endurance",
              "display_text": "Endurance"
            }
          }
        },
        {
          "item_stat": {
            "stat_amount": 6,
            "item_stat_type": {
              "name": "STAT_att_mastery",
              "display_text": "Mastery"
            }
          }
        }
      ],
      "enhancements": [
        {
          "item_enhancement_type": {
            "name": "Augment"
          }
        }
      ],
      "crafted_with_item_information": {
        "number_crafted_with_item": 0,
        "api_link": "http://swtordata.com/api/v2_2/item_crafted_withs/8479"
      },
      "rewarded_from_missions_information": {
        "number_of_missions_rewarded_from": 0,
        "api_link": "http://swtordata.com/api/v2_2/item_rewarded_from_missions/8479"
      },
      "crafted_by": [],
      "model_information": {
        "id": 258,
        "name": "/art/dynamic/weapon/model/electroblade_low05_a02_v01.gr2",
        "api_link": "http://swtordata.com/api/v2_2/item_models/258",
        "number_of_items_with_model": 21
      },
      "item_slots": [
        {
          "name": "EquipHumanMainHand"
        },
        {
          "name": "Inventory"
        },
        {
          "name": "Bank"
        },
        {
          "name": "Loot"
        },
        {
          "name": "Buyback"
        },
        {
          "name": "Any"
        }
      ],
      "required_profession": null
    },
    {
      "id": 70950,
      "stack_count": 0,
      "minimum_level": 0,
      "max_durability": 0,
      "value": 0,
      "display_name": "Title: Test Pilot",
      "description": "Grants your character the title \"Test Pilot\".",
      "giftrank": null,
      "imp_voice_modulation": null,
      "rep_voice_modulation": null,
      "armor_rating": null,
      "consumed_on_use": true,
      "unique_limit": 0,
      "required_gender": null,
      "social_score_required": false,
      "social_score_required_rank": 0,
      "required_valor_rank": 0,
      "required_profession_level": null,
      "api_link": "http://swtordata.com/api/v2_2/items/70950",
      "api_icon": "http://cdn.swtordata.com/icons/improvedpowerconversionmodule.png",
      "disassemble": "None",
      "binding": "OnPickup",
      "website_link": "http://swtordata.com/items/70950-title-test-pilot",
      "item_level": 0,
      "armor_spec": null,
      "enhancement_type": null,
      "gift_type": null,
      "quality": {
        "name": "Artifact",
        "darkdisplaycolor": "Purple",
        "lightdisplaycolor": "Purple",
        "darkdisplaycolor_hex": "a800d7",
        "lightdisplaycolor_hex": "a800d7"
      },
      "use_ability": {
        "id": 21926,
        "display_name": "Apply Title",
        "description": "Unlocks a title for this character.",
        "cooldown": 0,
        "forcecost": 0,
        "maxrange": 0,
        "passive": false,
        "hidden": false,
        "globalcooldown": -1,
        "overridegcd": null,
        "usespushback": false,
        "ignorealacrity": true,
        "loscheck": true,
        "energycost": 0,
        "targetarc": 0,
        "channeltime": 0,
        "casttime": 3,
        "minrange": 0,
        "ammoheatcost": 0,
        "targetarcoffset": 0,
        "api_link": "http://swtordata.com/api/v2_2/abilities/21926",
        "website_link": "http://swtordata.com/abilities/21926",
        "api_icon": "http://cdn.swtordata.com/icons/bravado.png",
        "category": "Mtx",
        "subcategory": "Unlock",
        "target_rule": {
          "name": "Self"
        },
        "get_tooltip": {
          "onmouseout": "ajax_hideTooltip();",
          "onmouseover": "ajax_showTooltip(window.event,(\"http://swtordata.com/widgettext.html?url='http://swtordata.com/abilities/21926'\"),this);return false;"
        },
        "items_use_and_equip_information": {
          "number_of_items_with_equip": 0,
          "number_of_items_with_use": 180
        }
      },
      "equip_ability": null,
      "category": "Spvp",
      "subcategory": "Title",
      "get_tooltip": {
        "onmouseout": "ajax_hideTooltip();",
        "onmouseover": "ajax_showTooltip(window.event,(\"http://swtordata.com/widgettext.html?url='http://swtordata.com/items/70950'\"),this);return false;"
      },
      "combined_stats": [],
      "base_stats": [],
      "enhancements": [],
      "crafted_with_item_information": {
        "number_crafted_with_item": 0,
        "api_link": "http://swtordata.com/api/v2_2/item_crafted_withs/70950"
      },
      "rewarded_from_missions_information": {
        "number_of_missions_rewarded_from": 0,
        "api_link": "http://swtordata.com/api/v2_2/item_rewarded_from_missions/70950"
      },
      "crafted_by": [],
      "model_information": null,
      "item_slots": [
        {
          "name": "Inventory"
        },
        {
          "name": "Bank"
        },
        {
          "name": "Loot"
        },
        {
          "name": "Buyback"
        },
        {
          "name": "Any"
        }
      ],
      "required_profession": null
    }
  ]
}

1 个答案:

答案 0 :(得分:1)

您的问题是因为您在$.each上调用data,这是一个对象。保存display_name值的属性位于data.objects数组中,因此您需要在其中循环:

$.each(data.objects, function(i, item) { // note data.objects here
    $("#results").append("<div>" + item.display_name + "<br><br></div>");
});

Working example

请注意,在上面的链接中,您无法从jsFiddle调用您的URL,因为它没有SSL证书,因此我对返回的对象进行了硬编码。