English 中文(简体)
我如何用j Query读写JSON的档案?
原标题:How do I use jQuery to read a file from JSON?

我正试图读写一个有 j子的JSON案:

       var listOfItems;
       $(function() {
            $.getJSON( myData.json , function(data) {
                listOfItems = data.items;
            });
        });

这是我的Data.json:

{
    {"source": "Microsoft", "target": "Amazon", "type": "licensing"},
    {"source": "Microsoft", "target": "HTC", "type": "licensing"},
    {"source": "Samsung", "target": "Apple", "type": "suit"},
    {"source": "Motorola", "target": "Apple", "type": "suit"}
}

但是, Chrome网络开发商指出,在本守则之后没有界定<代码>listOfItem。 为什么? 我相信, j、超文本和JSON的档案都属于同一目录。

最佳回答

它是一个不寻常的事件。 您不能像在偶然的情况下那样履行一项可变的任务,你必须履行你在具体职能范围内开展的任何工作。 否则,它将确保它举行一场同步的活动(通过在J Query确定正确的变量),届时,GET-request将在继续守则之前完成(未建议)。

问题回答

Fix the JSON. jsonlint,将告诉你什么是错的。

JSON的产权名称是插图(如Java编,可能为识别符号),因此,这些名称必须“<>>。

您在此提出的主要问题是,你使用“i”而不是“[”一阵列。

各位

[
    {source: "Microsoft", target: "Amazon", type: "licensing"},
    {source: "Microsoft", target: "HTC", type: "licensing"},
    {source: "Samsung", target: "Apple", type: "suit"},
    {source: "Motorola", target: "Apple", type: "suit"},
]

页: 1

这似乎含有一项目标,它本身包含4个物体,但这些内物体没有被给予财产名称。

这将有效

{
    "one"   : { "source":  "Microsoft", "target" : "Amazon", "type" : "licensing" },
    "two"   : { "source" : "Microsoft", "target" : "HTC",    "type" : "licensing" },
    "three" : { "source" : "Samsung",   "target" : "Apple",  "type" : "suit" },
    "four"  : { "source" : "Motorola",  "target" : "Apple",  "type" : "suit" }
}

但是,鉴于你的<代码>listOfItems,我不认为你想要什么。 你们可能想的是一件大事。

这样做的方法是使用方括号,而不是照相。

[
    { "source":  "Microsoft", "target" : "Amazon", "type" : "licensing" },
    { "source" : "Microsoft", "target" : "HTC",    "type" : "licensing" },
    { "source" : "Samsung",   "target" : "Apple",  "type" : "suit" },
    { "source" : "Motorola",  "target" : "Apple",  "type" : "suit" }
]

此外,在最后项目上观察这一拖车。 它在一些浏览器/javascript发动机中破碎。 如果听说过的话,我不知道。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.