Quiero guardar una serie de elementos clave de valor, una forma común de hacer esto podría ser algo como:Almacenamiento de una matriz de valores clave en una cadena JSON compacta
// 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" }
}
Pero, cuando hay muchos pares/artículos, la longitud de la cadena se convierte prohibidas, y quiero una manera compacta, esto podría ser un ejemplo:
// --> (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" }
}
Además, quiero una manera de identificar los datos como una matriz de valor clave, porque, es posible que quiera para almacenar otros datos en el mismo archivo JSON. tengo estos ejemplos:
// --> (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" }
}
¿Qué cosa, ¿cuál te sugieres, ¿tienes alguna otra manera? Gracias.
ACTUALIZACIÓN 1: Elimine el código no válido. Javascript => JSON
Actualización 2: Añadir datos de valor no clave
Actualización 3: Reemplazar "[" y "]" para "{" y "}" en cada par de valores clave
No hay una sola línea de JSON en su código. Solo objetos de JavaScript. –
@Felix Kling. Gracias. Elimino el código inválido que lo hace, como JS en lugar de JSon – umlcat
Todavía no es JSON. En JSON, cada tecla (y cadena) tiene que estar entre comillas dobles y algo como 'param =' no existe. –