将键值数组存储到紧凑的JSON字符串中 [英] Storing a Key Value Array into a compact JSON string

查看:145
本文介绍了将键值数组存储到紧凑的JSON字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

此外,我想要一种将数据标识为键值数组的方法, 因为,我可能想将其他数据存储在同一JSON文件中. 我有这些例子:

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:

// --> (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.

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

UPDATE 1: Remove invalid code. Javascript => JSON

更新2:添加非键值数据

UPDATE 2: Add non key value data

更新3:在每个键值对中将"{"和}"替换为"["和]"

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

推荐答案

如果解析此 的逻辑知道{"key": "slide0001.html", "value": "Looking Ahead"}是键/值对,则可以将其转换为数组,然后包含一些常量,用于指定哪个索引映射到哪个键.

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.

例如:

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.

这篇关于将键值数组存储到紧凑的JSON字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆