使用Jquery读取JSON

时间:2012-01-16 10:11:46

标签: jquery json

我创建了一个包含以下内容的JS文件;

{ "Root":{"APPLICATION":"TestMenu",
          "SUBROOT":["ADMIN","Seller","Buyer"]
       "TAB":"ADMIN":"Home","ADMIN":"LiveAuction","ADMIN":"Master","ADMIN":"PostAuction"]
                  "SUBTAB":[
                            {"APPLICATION":"TestMenu","SUBROOT":"ADMIN","TAB":"HOME","CHILD":"HOME","URL":"\Default.aspx"},
                                {"APPLICATION":"TestMenu","SUBROOT":"ADMIN","TAB":"LiveAuction","CHILD":"","URL":"#"},
                                {"APPLICATION":"TestMenu","SUBROOT":"ADMIN","TAB":"LiveAuction","CHILD":"AuctionCatalog","URL":"\AuctionCatalog.aspx"},
                                {"APPLICATION":"TestMenu","SUBROOT":"ADMIN","TAB":"LiveAuction","CHILD":"BidBook","URL":"\BidBook.aspx"},
                                {"APPLICATION":"TestMenu","SUBROOT":"ADMIN","TAB":"LiveAuction","CHILD":"SampleRequest","URL":"\SampleRequest.aspx"}
                           ]
           }
  }

我用以下代码调用该JS文件;

  $.getJSON("js/TestMenu.js", {}, function (json) { JsonCallBack(json); });
    function JsonCallBack(json) {
        $.each(json.results, function (i, tweet) {
            alert("JSON Data: " + json.Root);
        });
    }

现在我想在单个对象中使用JSON,我可以检索但是我没有得到那个JSON。

当我运行萤火虫时,我可以看到响应是一样的。谁能告诉我为什么这不起作用?

1 个答案:

答案 0 :(得分:1)

您的JSON无效。使用JSLint检查您的JSON是否有效。 JSLint说: 错误: 第1行第74个问题:预期'}'匹配第1行的'{',而不是'TAB'。

{ "Root":{"APPLICATION":"TestMenu", "SUBROOT":["ADMIN","Seller","Buyer"] "TAB...

第1行第79个问题:预期'}'匹配第1行的'{',而不是':'。

{ "Root":{"APPLICATION":"TestMenu", "SUBROOT":["ADMIN","Seller","Buyer"] "TAB...

第1行字符80处的问题:预期'(结束)'而是看到'ADMIN'。

{ "Root":{"APPLICATION":"TestMenu", "SUBROOT":["ADMIN","Seller","Buyer"] "TAB...

JSON:不好。