【问题标题】:Storing a Key Value Array into a compact JSON string将键值数组存储到紧凑的 JSON 字符串中
【发布时间】:2011-06-18 18:22:39
【问题描述】:

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

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

另外,我想要一种将数据标识为键值数组的方法, 因为,我可能想将其他数据存储在同一个 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:将每个键值对中的“[”和“]”替换为“{”和“}”

【问题讨论】:

    标签: json key-value


    【解决方案1】:

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

    var params = {
        'slide0001.html': 'Looking Ahead',
        'slide0002.html': 'Forecase',
        ...
    };
    
    return params['slide0001.html']; // returns: Looking Ahead
    

    【讨论】:

    • 这和第一个例子一样,但是,它不会识别出这是一个键值数组。
    【解决方案2】:

    如果解析此的逻辑知道{"key": "slide0001.html", "value": "Looking Ahead"} 是一个键/值对,那么您可以将其转换为一个数组并保存一些常量,指定哪个索引映射到哪个键。 p>

    例如:

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

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

    【讨论】:

    • 它的情况是“不提前知道数据的结构”。谢谢
    【解决方案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"
    

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-31
        • 2018-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多