使用 JQuery 加载 JSON 后,我得到一个而不是多个条目

发布于 2024-09-19 10:55:09 字数 724 浏览 10 评论 0原文

我构建了一些

{
    "News": {
         "Article": {"title": "test"},
         "Article": { "title": "test" },
         /*Snipped*/
         "Article": { "title": "test" },
         "Article": { "title": "test" }
    },
    "count": "20"
}

JSON 格式化程序 中验证的 JSON。但是当我尝试通过 jQuery 获取这些数据时,我没有得到预期的结果:

$.getJSON('php/json.php', function(data) {
  console.log(data);
});

结果:

News: Object
Article: Object
title: "test"
__proto__: Object
__proto__: Object
count: "20"

但是我的其他 19 个 Article 对象在哪里?我是 JSON 和 jQuery 的 getJSON 新手。我有什么遗漏的吗?

I've built some JSON

{
    "News": {
         "Article": {"title": "test"},
         "Article": { "title": "test" },
         /*Snipped*/
         "Article": { "title": "test" },
         "Article": { "title": "test" }
    },
    "count": "20"
}

which validates in a JSON formatter. But when I try to ingest this data through jQuery I don't get what's expected:

$.getJSON('php/json.php', function(data) {
  console.log(data);
});

Results:

News: Object
Article: Object
title: "test"
__proto__: Object
__proto__: Object
count: "20"

But where are my 19 other Article objects? I'm new to JSON and jQuery's getJSON. Is there anything I'm missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2024-09-26 10:55:09

JavaScript 对象是字典,这意味着名称必须是唯一的。您正在复制 Article,因此每个实例都会覆盖前一个实例(或被忽略;我不知道解析器采用哪个路径)。

要解决此问题,您需要将此名称定义为引用数组:

{ "News": { "Article": [
                { "title": "test" }, 
                { "title": "test" }, 
                ...], 
             "count": "20" }

但是您可以进一步减少此名称,假设所有新闻项都是文章,因为 count 变得多余:

{ "News": [
          { "title": "test" }, 
          { "title": "test" }, 
          ...] }

JavaScript objects are dictionaries, which means that the name must be unique. You're replicating Article, so each instance overwrites the previous (or is ignored; I don't know which path the parser takes).

To fix, you need to define this name as referencing an array:

{ "News": { "Article": [
                { "title": "test" }, 
                { "title": "test" }, 
                ...], 
             "count": "20" }

But you could probably reduce this further, assuming that all news items are articles, because count becomes superfluous:

{ "News": [
          { "title": "test" }, 
          { "title": "test" }, 
          ...] }
世界等同你 2024-09-26 10:55:09

更好的格式是

{
    "News": {
         "1": {"title": "test","type":"article"},
         "2": {"title": "test","type":"article"},
         /*Snipped*/
         "19": {"title": "test","type":"article"},
         "20": {"title": "test","type":"article"}
    },
    "count": "20"
}

So you can do with every。

$.each(Object.news,function(id,ObjectItem){
    if(ObjectItem.type == 'article')
    {
        //do something with ObjectItem.title
    }
})

A better format would be

{
    "News": {
         "1": {"title": "test","type":"article"},
         "2": {"title": "test","type":"article"},
         /*Snipped*/
         "19": {"title": "test","type":"article"},
         "20": {"title": "test","type":"article"}
    },
    "count": "20"
}

So you can do with each.

$.each(Object.news,function(id,ObjectItem){
    if(ObjectItem.type == 'article')
    {
        //do something with ObjectItem.title
    }
})
永言不败 2024-09-26 10:55:09

您多次定义了Article键。这不是语法错误(这就是格式化程序不抱怨的原因),而是逻辑错误。您应该使用数组代替。

You defined the Article-key multiple times. This is not a syntax error (which is why the formatter does not complain) but rather a logical one. You should use an array instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文