将键值数组存储到紧凑的JSON字符串中

时间:2011-06-18 18:22:40

标签: json key-value

我想存储一组键值项,常见的方法可能是:

// 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" }
}

此外,我想要一种方法将数据识别为keyvalue数组, 因为,我可能想将其他数据存储在同一个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

更新2:添加非密钥值数据

更新3:将“[”和“]”替换为每个键值对中的“{”和“}”

4 个答案:

答案 0 :(得分:16)

那你为什么不简单地使用键值文字呢?

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

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

答案 1 :(得分:13)

如果解析此的逻辑知道 {"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"]
  ]
}

...然后由解析器处理。

答案 2 :(得分:3)

对我来说,这是在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"

当然,第一个元素或第二个元素也可以描述为对象本身,每个元素都有三个元素。

答案 3 :(得分: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);