0

您好,我是使用 jQuery 读取 JSON 文件的新手。我正在使用以下代码:

  $.getJSON('updateProfile.js', function(data) {
    var Options = [];
    $.each(data.Options, function(key, val) {
    alert('we have data for ' + val.id + ' and ' + val.label);
     });

我使用了 JSON 验证工具,并且文件有效。我在 FF 中使用了 chrome 和 firebug 来确认文件的状态为 200。但是,当我在 .getJSON 调用上设置断点时,代码会被绕过。

我使用了一个本地 tomcat 服务器,并将这个放在一个实际的服务器上,所有这些都以相同的方式执行。

这是我要阅读的 JSON

{"Options":[
"title": "User Profile",
"description": "Your preferences can be modified below. Click on the section on the left. This will present the details on the right. Once you have updated your profile, click the Save button to save your changes.",
"id": "userprofileStackTabs",
"children": [
    {
        "id": "up001",
        "linkID": "namePhones",
        "label": "Name and Phones",
        "description": "",
        "default": "true"
    },
    {
        "id": "up002",
        "linkID": "password",
        "label": "Password",
        "description": " "
    },
    {
        "id": "up003",
        "linkID": "physAddrss",
        "label": "Physical Address",
        "description": " "
    },
    {
        "id": "up004",
        "linkID": "dfShippingAddrss",
        "label": "Default Shipping Address",
        "description": " "
    },
    {
        "id": "up005",
        "linkID": "cpgnPref",
        "label": "Campaign Preferences",
        "description": " "
    },
    {
        "id": "up006",
        "linkID": "appPref",
        "label": "Application Preferences",
        "description": " "
    },
    {
        "id": "up007",
        "linkID": "langPref",
        "label": "Language Preferences",
        "description": " "
    },
    {
        "id": "up008",
        "linkID": "agentLic",
        "label": "Agent Licenses",
        "description": " "
    },
    {
        "id": "up009",
        "linkID": "searchPref",
        "label": "Search Preferences",
        "description": " "
    },
    {
        "id": "up010",
        "linkID": "socMedAcct",
        "label": "Social Media Accounts",
        "description": " "
    }
],
"buttons": [
    {
        "id": "upButn_Save",
        "label": "Save"
    },
    {
        "id": "upButn_Cancel",
        "label": "Cancel"
    }
]

]}

任何帮助将不胜感激。

4

1 回答 1

0

我不知道你为什么要在 $.get 中创建 Options 数组。我认为这是不需要的,你根本没有使用它......

尝试这个

$.each(data.Options[0].children, function(key, val) {
   alert('we have data for ' + val.id + ' and ' + val.label);
 });

你很想得到选项数组内的孩子的 id 和标签[0]..所以循环遍历 obecjt 的 chilren 数组并获取它..

于 2013-03-19T20:12:10.293 回答