5

我正在 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"
        ]
    }
}
}

所以对于每一个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;

};

这很好用,除了我想存储为非字符串(整数、数字或日期时间)的字段,就像上面示例中的年份一样,最终在我的索引中作为字符串。

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

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

谢谢!

4

3 回答 3

10

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

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

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

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

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

于 2013-02-04T13:12:26.087 回答
1

不,你不能,在 JSON 中键是字符串。

您可以做的最好的事情是存储这些键的字符串表示形式,无论是整数还是对象(更复杂,您必须构建一个序列化函数)。

如果您只想使用从 0 开始的连续整数键,则可以使用数组。

于 2013-02-04T13:14:57.913 回答
1

根据json spec,您可以在任何可以有值的地方有一个数字。所以对象的键必须是字符串,但值可以是数字。此外,数组中的任何值都可以是数字。

不过,规范并不重要;我相信问题是这一行:

index.current[label][value].push(doc._id);

当您阅读doc._id时,那是一个字符串。如果要将其作为数字存储在 JSON 中,则需要对其进行强制转换:

index.current[label][value].push(parseInt(doc._id, 10));

另请注意,将数字作为 ID 不是有效的 HTML

于 2013-02-04T13:16:59.290 回答