Ich erstelle eine Indexdatei in JSON, die ich als Datenbankindex für eine JavaScript-Anwendung verwende, an der ich gerade arbeite.Ist es möglich, ganze Zahlen als Schlüssel in einem JavaScript-Objekt zu speichern?
wird mein Index wie folgt aussehen:
{
"_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"
]
}
}
}
Also für jeden keyword
(wie Pulp Fiction), ich bin die passende document-id(s)
speichern.
Mein Problem ist mit Integer/Zahlen/Nicht-String-Daten, wie das Release-Jahr im obigen Beispiel. Dies wird als String gespeichert, während ich gehofft hatte, dass es als Nummer gespeichert wird.
Ich erstelle die Indexeinträge wie folgt aus:
// 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;
};
Das funktioniert gut, außer dass Felder, die ich als Nicht-Strings (Ganzzahlen, Zahlen oder Datetime) speichern möchte, wie das Jahr im obigen Beispiel als Strings in meinem Index enden.
Frage:
Ist es überhaupt möglich ist "non-string" Typen in einem JSON-Dokument zu speichern? Wenn ja, kann ich den Schlüssel eines Schlüssel/Wert-Paares auch als "Nicht-String" -Element speichern.
Wenn nicht, müsste ich meinen Indexdefinitionen einen Parameter hinzufügen, der den Typ jedes Schlüssels deklariert, um den Schlüssel-String zu ändern, wenn ich darauf stoße, oder gibt es einen besseren Weg dazu?
Danke!
entsprechend der [json spec] (http://json.org/) können Sie eine Nummer überall haben, wo Sie einen 'Wert' haben könnten. Also muss der Schlüssel eines Objekts * eine Zeichenkette sein, aber der Wert kann eine Zahl sein. Jeder der Werte in einem Array kann auch eine Zahl sein. – jbabey