是否可以将整数存储为javascript对象中的键?

fre*_*ent 5 javascript indexing jquery json types

我正在用JSON创建一个索引文件,我正在使用它作为我正在处理的javascript应用程序的数据库索引.

我的索引将如下所示:

{
"_id": "acomplex_indices.json",
"indexAB": {
    "title": {
        "Shawshank Redemption": [
            "0"
        ],
        "Godfather": [
            "1"
        ],
        "Godfather 2": [
            "2"
        ],
        "Pulp Fiction": [
            "3"
        ],
        "The Good, The Bad and The Ugly": [
            "4"
        ],
        "12 Angry Men": [
            "5"
        ],
        "The Dark Knight": [
            "6"
        ],
        "Schindlers List": [
            "7"
        ],
        "Lord of the Rings - Return of the King": [
            "8"
        ],
        "Fight Club": [
            "9"
        ],
        "Star Wars Episode V": [
            "10"
        ],
        "Lord Of the Rings - Fellowship of the Ring": [
            "11"
        ],
        "One flew over the Cuckoo's Nest": [
            "12"
        ],
        "Inception": [
            "13"
        ],
        "Godfellas": [
            "14"
        ]
    },
    "year": {
        "1994": [
            "0",
            "3"
        ],
        "1972": [
            "1"
        ],
        "1974": [
            "2"
        ],
        "1966": [
            "4"
        ],
        "1957": [
            "5"
        ],
        "2008": [
            "6"
        ],
        "1993": [
            "7"
        ],
        "2003": [
            "8"
        ],
        "1999": [
            "9"
        ],
        "1980": [
            "10"
        ],
        "2001": [
            "11"
        ],
        "1975": [
            "12"
        ],
        "2010": [
            "13"
        ],
        "1990": [
            "14"
        ]
    }
}
}
Run Code Online (Sandbox Code Playgroud)

所以对于每一个keyword(像纸浆小说),我都存储匹配document-id(s).

我的问题是整数/数字/非字符串数据,如上例中的发布年份.它存储为字符串,而我希望它将存储为数字.

我正在创建这样的索引条目:

// indices = the current index file
// doc = the document to update the index with
// priv.indices = all indices defined for this application instance
// priv.indices.fields = index fields e.g. "year", "director", "title"
// priv.indices.name = name of this index

priv.updateIndices = function (indices, doc) {
var i, j, index, value, label, key, l = priv.indices.length;

// loop all indices to add document
for (i = 0; i < l; i += 1) {
  index = {};
  index.reference = priv.indices[i];
  index.reference_size = index.reference.fields.length;
  index.current = indices[index.reference.name];

  for (j = 0; j < index.reference_size; j += 1) {
    label = index.reference.fields[j];    // like "year"
    value = doc[label];                   // like 1985

    // if document has a label field (e.g. doc.year = 1985)
    if (value !== undefined) {

      // check if the index file already contains an entry for 1985
      index.current_size = priv.getObjectSize(index.current[label]);

      if (index.current_size > 0) {
        // check if the document id is already in the index
        // in case the data is updated (e.g. change 1982 to 1985)
        key = priv.searchIndexByValue(
          index.current[label],
          doc._id,
          "key"
        );
        if (!!key) {
          delete index.current[label][key];
        }
      }
      // create a new array if 1985 is not in the index yet
      if (index.current[label][value] === undefined) {
        index.current[label][value] = [];
      }
      // add the document id to an existing entry
      index.current[label][value].push(doc._id);
    }
  }
}
return indices;
Run Code Online (Sandbox Code Playgroud)

};

这样可以正常工作,除了我想要存储为非字符串(整数,数字或日期时间)的字段,就像上面示例中的年份最终作为索引中的字符串一样.

问题:
是否可以在JSON文档中存储"非字符串"类型?如果是这样,我还可以将键/值对的键存储为"非字符串"元素.

如果没有,我是否必须在索引定义中添加一个参数,声明每个键的类型,以便在我遇到它时修改键字符串,或者有更好的方法吗?

谢谢!

Jam*_*ice 10

是否可以在JSON文档中存储"非字符串"类型?

是.属性的值可以是字符串,数字,布尔值,对象,数组或null(undefined是一个值得注意的例外 - 它是本机JavaScript类型,但它不是有效的JSON值).

我还可以将键/值对的键存储为"非字符串"元素吗?

不可以.密钥名称必须始终为字符串.但是,这并不意味着您无法将该字符串解析为其他JavaScript类型.例如,如果您有一个字符串但需要一个数字,则可以使用该parseInt函数或一元运算+符.

有关更多详细信息,请参阅JSON语法.