将键值数组存储到压缩JSON字符串中

ruyhziif  于 2023-03-20  发布在  其他
关注(0)|答案(5)|浏览(140)

我想存储一个键值项的数组,一种常见的方法是:

// 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
UPDATE 2:添加非键值数据
更新3:将每个键值对中的“{”和“}”替换为“[”和“]”

u3r8eeie

u3r8eeie1#

那么,为什么不简单地使用键值文本呢?

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

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

r8xiu3jd2#

如果解析这个的逻辑 * 知道 * {"key": "slide0001.html", "value": "Looking Ahead"}是一个键/值对,那么您可以将它转换成一个数组,并保存几个常量来指定哪个索引Map到哪个键。
例如:

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

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

b5lpy0ml

b5lpy0ml3#

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

当然,第一元素或第二元素可替换地被描述为对象本身,每个对象具有三个元素。

zfycwa2u

zfycwa2u4#

对于在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);
klsxnrf1

klsxnrf15#

2023年解决方案:使用对象.fromEntries()

Object.fromEntries([{id: 1}, {id: 2}].map(item => ([item.id, item])));

返回

{
    "1": {
        "id": 1
    },
    "2": {
        "id": 2
    }
}

相关问题