.getJSON不处理文件

时间:2013-03-19 20:03:50

标签: jquery json getjson

你好我是新手使用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"
    }
]

]}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

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

试试这个

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

你要获取子元素的id和标签,这些子元素位于Options数组[0]中..所以循环遍历obecjt的chilren数组并得到它..