将键值数组存储到紧凑的 JSON 字符串中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6397990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing a Key Value Array into a compact JSON string
提问by umlcat
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:
此外,我想要一种将数据标识为键值数组的方法,因为我可能希望将其他数据存储在同一个 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:在每个键值对中将“[”和“]”替换为“{”和“}”
采纳答案by rid
If the logic parsing this knowsthat {"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", "value": "Looking Ahead"}是一个键/值对,那么你可以将它转换成一个数组并保存一些常量来指定哪个索引映射到哪个键。
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.
...然后由解析器处理。
回答by Crozin
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
回答by Programmer Bruce
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.
第一元素或第二元素也可以被描述为对象本身,当然,每个元素具有三个元素。
回答by morteza ataiy
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);

