2011-06-18 55 views
9

我想存儲鍵值項的數組,做一個共同的方式,這可能是這樣的:存儲密鑰值數組成一個緊湊的JSON字符串

// the JSON data may store several data types, not just key value lists, 
// but, must be able to identify some data as a key value list 

// --> more "common" way to store a key value array 
{ 
    [ 
    {"key": "slide0001.html", "value": "Looking Ahead"}, 
    {"key": "slide0008.html", "value": "Forecast"}, 
    {"key": "slide0021.html", "value": "Summary"}, 
    // another THOUSANDS KEY VALUE PAIRS 
    // ... 
    ], 
    "otherdata" : { "one": "1", "two": "2", "three": "3" } 
} 

但是,當有許多對/的物品,該字符串長度變禁止, 和我希望有一個緊湊的方式,這可能是一個例子:

// --> (1) a "compact" way to store a key value array 
{  
    [ 
     {"slide0001.html", "Looking Ahead"}, 
     {"slide0008.html", "Forecast"}, 
     {"slide0021.html", "Summary"}, 
     // another THOUSANDS KEY VALUE PAIRS 
     // ... 
    ], 
    "otherdata" : { "one": "1", "two": "2", "three": "3" } 
} 

此外,我想辦法來識別數據爲密鑰值陣列, 因爲,我可能要將其他數據存儲在同一個JSON文件中。 我有這方面的例子:

// --> (2) a "compact" way to store a key value array  
{ 
    "keyvaluelist": 
    [ 
     {"slide0001.html", "Looking Ahead"}, 
     {"slide0008.html", "Forecast"}, 
     {"slide0021.html", "Summary"}, 
     // another THOUSANDS KEY VALUE PAIRS 
     // ... 
    ], 
    "otherdata" : { "one": "1", "two": "2", "three": "3" } 
} 

// --> (3) a "compact" way to store a key value array  
{ 
    "mylist": 
    { 
     "type": "keyvaluearray", 
    "data": 
    [ 
     {"slide0001.html", "Looking Ahead"}, 
     {"slide0008.html", "Forecast"}, 
     {"slide0021.html", "Summary"}, 
        // another THOUSANDS KEY VALUE PAIRS 
        // ... 
    ] 
    }, 
    "otherdata" : { "one": "1", "two": "2", "three": "3" } 
} 

你有什麼建議,你有你的東西,其中之一另一種方式? 謝謝。

更新1:刪除無效的代碼。的Javascript => JSON

UPDATE 2:添加非關鍵值數據

UPDATE 3:替換 「[」 和 「]」 爲 「{」 和 「}」 中的每個密鑰值對

+0

代碼中沒有一行JSON。只有JavaScript對象。 –

+0

@Felix Kling。謝謝。我刪除使它成爲無效的代碼,如JS而不是JSon – umlcat

+0

它仍然不是JSON。在JSON中,每個鍵(和字符串)都必須用雙引號引起來,而'param ='不存在。 –

回答

8

如果解析此知道{"key": "slide0001.html", "value": "Looking Ahead"}是一個鍵/值對的邏輯,則可以在一個陣列變換它並按住幾常數指定哪些索引映射到了哪個鍵。

例如:

var data = ["slide0001.html", "Looking Ahead"]; 

var C_KEY = 0; 
var C_VALUE = 1; 

var value = data[C_VALUE]; 

所以,現在,你的數據可以是:

[ 
    ["slide0001.html", "Looking Ahead"], 
    ["slide0008.html", "Forecast"], 
    ["slide0021.html", "Summary"] 
] 

如果您解析邏輯不提前瞭解數據的結構,可以添加一些元數據來描述它。例如:

{ meta: { keys: [ "key", "value" ] }, 
    data: [ 
    ["slide0001.html", "Looking Ahead"], 
    ["slide0008.html", "Forecast"], 
    ["slide0021.html", "Summary"] 
    ] 
} 

...然後將由解析器處理。

+0

它的情況「事先並不知道數據的結構」。謝謝 – umlcat

11

那麼,爲什麼你不只是使用鍵值文字?

var params = { 
    'slide0001.html': 'Looking Ahead', 
    'slide0002.html': 'Forecase', 
    ... 
}; 

return params['slide0001.html']; // returns: Looking Ahead 
+0

它將像第一個例子,但它不會識別這是一個關鍵值數組。 – umlcat

1

對我來說,這是在JSON中構造這樣的數據的最「自然」的方法,只要所有的鍵都是字符串。

{ 
    "keyvaluelist": { 
     "slide0001.html": "Looking Ahead", 
     "slide0008.html": "Forecast", 
     "slide0021.html": "Summary" 
    }, 
    "otherdata": { 
     "one": "1", 
     "two": "2", 
     "three": "3" 
    }, 
    "anotherthing": "thing1", 
    "onelastthing": "thing2" 
} 

我看這是

a JSON object with four elements 
    element 1 is a map of key/value pairs named "keyvaluelist", 
    element 2 is a map of key/value pairs named "otherdata", 
    element 3 is a string named "anotherthing", 
    element 4 is a string named "onelastthing" 

第一元件或第二元件可以可選地被描述爲對象自身,當然,每個三個元素。

0

對於使用鍵/值對的JSON使用對象,不使用數組

查找名稱/數組值很辛苦,但在物體很容易

例:

var exObj = { 
 
    "mainData": { 
 
    "slide0001.html": "Looking Ahead", 
 
    "slide0008.html": "Forecast", 
 
    "slide0021.html": "Summary", 
 
    // another THOUSANDS KEY VALUE PAIRS 
 
    // ... 
 
    }, 
 
    "otherdata" : { "one": "1", "two": "2", "three": "3" } 
 
}; 
 
var mainData = exObj.mainData; 
 
// for use: 
 
Object.keys(mainData).forEach(function(n,i){ 
 
    var v = mainData[n]; 
 
    console.log('name' + i + ': ' + n + ', value' + i + ': ' + v); 
 
}); 
 

 
// and string length is minimum 
 
console.log(JSON.stringify(exObj)); 
 
console.log(JSON.stringify(exObj).length);

相關問題