【问题标题】:Google Chrome Apps: chrome.storage.localGoogle Chrome 应用程序:chrome.storage.local
【发布时间】:2013-09-10 16:08:38
【问题描述】:

我很难找出为什么在我正在开发的应用程序中会发生以下情况:

所以我读了一个 JSON 文件,我想在其中读取他的内容并将其保存在 localStorage 上,在 chrome 应用程序中是 chrome.storage.local

所以我从一个 json 文件中得到以下信息:

{
  "schedule": {
    "id": "416a18d6-7e42-4f8a-ac24-e902abe76f39",
    "name": "blabla",
    "updatedOn": "2013-09-08T16:34:41.000Z",
    "etag": "esxrdctfvygbuhnij5464df7g8hu",
    "applications": [
      {
        "id": "app001",
        "type": "text/html",
        "src": "http://www.google.com"
      },
      {
        "id": "app002",
        "type": "text/html",
        "src": "http://www.stackoverflow.com"
      }
    ],
    "normalContent": [
      {
        "layout_id": "layout001",
        "layout_name": "layout1",
        "layout_dur": "indefinite",
        "regions": [
          {
            "region_id": "region001",
            "region_name": "regiao1",
            "left": "0",
            "top": "0",
            "width": "0.5",
            "height": "1",
            "minWidth": "0",
            "minHeight": "0",
            "scheduleItem": "container",
            "limitCycle": "1",
            "selector": "seq",
            "containerList": [
              {
                "cid": "app001",
                "dur": "25"
              },
              {
                "cid": "app002",
                "dur": "15"
              }
            ]
          },
          {
            "region_id": "region002",
            "region_name": "regiao2",
            "left": "0.5",
            "top": "0",
            "width": "0.5",
            "height": "1",
            "minWidth": "0",
            "minHeight": "0",
            "scheduleItem": "container",
            "limitCycle": "1",
            "selector": "seq",
            "containerList": [
              {
                "cid": "app002",
                "dur": "15"
              },
              {
                "cid": "app001",
                "dur": "15"
              }
            ]
          }
        ]
      }
    ]
  }
}

所以我得到了这个,然后我使用以下方法将内容保存在存储中:

chrome.storage.local.set({playerSchedule:this.schedule}, function(){
    console.log('saved!')       
});

稍后当我阅读使用存储的内容时:

chrome.storage.local.get(["playerSchedule", "playerScheduleType"], function (res) {
    self.currentSchedule = res.playerSchedule;
    self.scheduleType = res.playerScheduleType;
});

这是我得到的:

{
  "schedule": {
    "applications": [
      {
        "id": "app001",
        "type": "text/html",
        "src": "http://www.google.com"
      },
      {
        "id": "app002",
        "type": "text/html",
        "src": "http://www.stackoverflow.com"
      }
    ],
    "etag": "esxrdctfvygbuhnij5464df7g8hu",
    "id": "416a18d6-7e42-4f8a-ac24-e902abe76f39",
    "name": "blabla",
    "normalContent": [
      {
        "layout_dur": "indefinite",
        "layout_id": "layout001",
        "layout_name": "layout1",       
        "regions": [
          {
            ...
            "containerList": [
            ]
          },
          {
            ...
            "containerList": [
            ]
          }
        ]
      }
    ],
    "updatedOn": "2013-09-08T16:34:41.000Z"    
  }
}

如您所见,我阅读的内容按字母顺序排列,而 containerList 中的内容为空。为什么会发生这种情况?我通过将对象转换为字符串并存储它来解决这个问题,但我想知道为什么保存为对象我是从作为对象读取得到的,还是我遗漏了什么?

提前致谢。

【问题讨论】:

  • 字母顺序符合标准。 JavaScript 对象的属性没有顺序,因此它们可以按任何顺序重新排列。不过,不知道为什么containerList 的内容消失了。
  • FWIW 我做了一个 JSON.parse(JSON.stringify(stuff in your first sample)),结果很干净。 (“干净” == containerList 的东西还在。)

标签: javascript json google-chrome-app


【解决方案1】:

这可能是http://code.google.com/p/chromium/issues/detail?id=277647。 Chrome 31 正在修复中。

【讨论】:

  • 欢迎来到 Stack Overflow!您似乎已经正确识别了这个问题。但是,Stack Overflow 上的答案应该不仅仅是一个链接,而且应该独立存在。请edit 您的回答包含有关该错误的具体性质的更多信息——即chrome.storage 似乎只接受10 级对象嵌套。一旦你这样做,我会很乐意为你的答案投票。
猜你喜欢
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
相关资源
最近更新 更多