Eventuali duplicati:
Is a way that use var to create JSON object in key?Come utilizzare una variabile String come chiave in un oggetto JSON
Vorrei costruire un oggetto JSON in JavaScript utilizzando il valore di un (String) variabile come chiave. Ma quello che ho ottenuto è il nome della variabile come chiave.
example.js:
function constructJson(jsonKey, jsonValue){
var jsonObj = { "key1":jsonValue, jsonKey:2};
return jsonObj;
}
La chiamata
constructJson("key2",8);
restituisce un JSON -> { "key1": 8, "jsonKey": 2} ma mi piacerebbe avere {" key1 ": 8," key2" : 2}.
Qualcuno sa come ottenere questo? sembra un problema semplice, ma non riesco a trovare la soluzione
thx in anticipo, ronny
dispiace per duplicato domanda ... trovato awnser qui: http://stackoverflow.com/a/882749/1005072 – rontron