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

时间:2021-03-05 20:52:26

I want to store an array of key value items, a common way to do this could be something like:

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

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

But, when there is many pairs / items, the string length becomes prohibited, and I want a compact way, this could be an example:

但是,当有许多对/项时,字符串长度就被禁止了,我想要一个紧凑的方法,这可以是一个例子:

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

Additionally, I want a way to identify the data as a keyvalue array, because, I may want to store other data in the same JSON file. I have these examples:

此外,我想要一种将数据标识为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" }
}

What do you thing, which one do you suggest, do you have another way ? Thanks.

你有什么建议,你有别的办法吗?谢谢。

UPDATE 1: Remove invalid code. Javascript => JSON

更新1:删除无效代码。Javascript = > JSON

UPDATE 2: Add non key value data

更新2:添加非键值数据

UPDATE 3: Replace "[" and "]" for "{" and "}" in each key value pair

更新3:在每个键值对中替换“{”和“}”

4 个解决方案

#1


9  

If the logic parsing this knows that {"key": "slide0001.html", "value": "Looking Ahead"} is a key/value pair, then you could transform it in an array and hold a few constants specifying which index maps to which key.

如果逻辑解析器知道{"key": "slide0001。“html”、“值”:“展望未来”}是一个键/值对,然后您可以在一个数组中对它进行转换,并保存一些常量,指定哪个索引映射到哪个键。

For example:

例如:

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

var C_KEY = 0;
var C_VALUE = 1;

var value = data[C_VALUE];

So, now, your data can be:

现在,你的数据可以是:

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

If your parsing logic doesn't know ahead of time about the structure of the data, you can add some metadata to describe it. For example:

如果解析逻辑事先不知道数据的结构,可以添加一些元数据来描述它。例如:

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

... which would then be handled by the parser.

…然后由解析器处理。

#2


13  

So why don't you simply use a key-value literal?

那么为什么不直接使用键值文字呢?

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

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

#3


2  

To me, this is the most "natural" way to structure such data in JSON, provided that all of the keys are strings.

对我来说,如果所有键都是字符串,这是用JSON构造此类数据的最“自然”方式。

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

I read this as

我读这个

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"

The first element or second element could alternatively be described as objects themselves, of course, with three elements each.

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

#4


0  

For use key/value pair in json use an object and don't use array

对于json使用的键/值对,使用一个对象,不要使用数组。

Find name/value in array is hard but in object is easy

在数组中查找名称/值是困难的,但在对象中是容易的。

Ex:

例:

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);

#1


9  

If the logic parsing this knows that {"key": "slide0001.html", "value": "Looking Ahead"} is a key/value pair, then you could transform it in an array and hold a few constants specifying which index maps to which key.

如果逻辑解析器知道{"key": "slide0001。“html”、“值”:“展望未来”}是一个键/值对,然后您可以在一个数组中对它进行转换,并保存一些常量,指定哪个索引映射到哪个键。

For example:

例如:

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

var C_KEY = 0;
var C_VALUE = 1;

var value = data[C_VALUE];

So, now, your data can be:

现在,你的数据可以是:

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

If your parsing logic doesn't know ahead of time about the structure of the data, you can add some metadata to describe it. For example:

如果解析逻辑事先不知道数据的结构,可以添加一些元数据来描述它。例如:

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

... which would then be handled by the parser.

…然后由解析器处理。

#2


13  

So why don't you simply use a key-value literal?

那么为什么不直接使用键值文字呢?

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

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

#3


2  

To me, this is the most "natural" way to structure such data in JSON, provided that all of the keys are strings.

对我来说,如果所有键都是字符串,这是用JSON构造此类数据的最“自然”方式。

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

I read this as

我读这个

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"

The first element or second element could alternatively be described as objects themselves, of course, with three elements each.

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

#4


0  

For use key/value pair in json use an object and don't use array

对于json使用的键/值对,使用一个对象,不要使用数组。

Find name/value in array is hard but in object is easy

在数组中查找名称/值是困难的,但在对象中是容易的。

Ex:

例:

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);