2013-02-04 14 views
5

JSONでインデックスファイルを作成していますが、これは私が作業しているjavascriptアプリケーションのソートオブデータベースインデックスとして使用しています。整数をキーとして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文書で「非文字列」型を格納することで、すべての可能ですか?その場合は、キー/値のペアのキーを「非文字列」要素として格納することもできます。

もしそうでなければ、キーストリングを実行するときにキーストリングを変更するために各キーのタイプを宣言するインデックス定義にパラメータを追加する必要がありますか?

ありがとうございます!

+1

[json spec](http://json.org/)によれば、あなたはどこでも数値を持つことができます。したがって、オブジェクトのキー*は文字列でなければなりませんが、値は数値にすることができます。また、配列内のどの値も数値にすることができます。 – jbabey

答えて

9

"非文字列"型をJSONドキュメントに格納することは可能ですか?

はい。プロパティの値は、文字列、数値、ブール値、オブジェクト、配列、またはヌル(undefinedは注目すべき例外です。これはネイティブJavaScriptタイプですが、有効なJSON値ではありません)。

キーと値のペアのキーを「非文字列」要素として保存することはできますか?

いいえ。キー名は常に文字列でなければなりません。ただし、その文字列を他のJavaScriptタイプに解析できないわけではありません。たとえば、文字列がありますが番号が必要な場合は、parseInt関数または単項式+演算子を使用できます。

詳細はJSON grammarを参照してください。

+0

+1適切な文法へのリンクを含む包括的な答え。 – Chad

1

いいえ、JSONではキーは文字列です。

あなたができることは、それらのキー、より大きい整数またはオブジェクト(より複雑で、直列化機能を構築する必要があります)の文字列表現を格納することです。

0から始まる連続する整数キーのみを使用する場合は、配列を使用できます。

1

json specによれば、値を持つことができる任意の場所に番号を付けることができます。したがって、オブジェクトのキーは文字列でなければなりませんが、値は数値にすることができます。また、配列内のどの値も数値にすることができます。

スペックはポイントの横にあります。

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

doc._idを読むと、それは文字列です。あなたは数としてJSONでそれを保存したい場合、あなたはそれをキャストする必要があります。

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

just numbers as IDs is not valid HTMLを持つことに注意してください。

+0

良いアイデア。しかし、問題は「値」が文字列であり、数値であることです。 – frequent

関連する問題