JSON Object Object Object

Asked

Viewed 2,211 times

0

I need to create a json object from scratch where each key has several values/values example:

 '{"name":["daniel","dias"],"morada":["joao","allen"]}'

For that I am executing this code several times, but the result is presented with numerical generated Keys:

facetaPush - key name

Response - array with desired values

valuesFacetas - save json object final

var json_string = "";

json_string +='{\"'+facetaPush+'\":[' ;
for (var i = 0; i < response.length-1; i++) {
    json_string +='\"'+response[i]+'\",';
}
json_string +='\"'+response[response.length-1]+'\"]}';

var json_obj = JSON.parse( json_string );
valuesFacetas[facetaPush].push(json_obj);
alert("valores facetas: "+valuesFacetas[facetaPush][facetaPush]);
  • edited question

  • 1

    What is the reason for mounting JSON to a string? What do you mean by "key is dynamic so create the object statically"? can you give an example?

4 answers

2

A quick way to solve this is by using the JSON.stringfy it accepts as argument any value and the serializes in JSON.

See the following example:

var obj = {
    name: ["daniel","dias"],
    morada: ["joao","allen"]
}

JSON.stringify(obj)
// Resultado: "{"name":["daniel","dias"],"morada":["joao","allen"]}"
  • key is dynamic so create the object statically do not know if it is possible

1

I believe it’s something similar to what you want:

function createMyObject(myKey, myCollection) {
   var myObj = new Object();
       myObj[myKey] = myCollection;
   return myObj;
}
var objName = createMyObject("name", ["daniel","dias"]);
var objMorada = createMyObject("morada", ["joao", "allen"]);

var all = createMyObject("myObjects", [objName, objMorada]);

console.log(objName, objMorada, all)

0

If this is your standard Objeto -> Array you can do one for to grab the keys and the other for to scroll through the values of this array.

var json = {
  "name": ["daniel", "dias"],
  "morada": ["joao", "allen"]
};

console.log(json);

for (key in json) {
  console.log(key); // Pega chaves [name,morada]
  for (i = 0; i < json[key].length; i++) {
    console.log(json[key][i]); // [pega valores dos arrays dentro de name,morada]
  }
}

0

The JSON is the representation String of the Javascript object. It’s easier to manipulate it as an object.

You can access an object property in several ways:

objeto.chave

or

objeto['chave']

now we can exchange for a variable

var varChave = 'chave';
objeto[varChave]

I made some examples to see if you find the best way in your case:

// suas variáveis
var facetaPush = 'name';
var response = ['daniel', 'dias'];

var objeto = {};
objeto[facetaPush] = response; // exemplo com variáveis
objeto['morada'] = ['joao', 'allen']; // exemplo estático
objeto.morada = ['joao', 'allen']; // exemplo estático

See how JSON would look:

var json = JSON.stringify(objeto);
// Resultado: "{"name":["daniel","dias"],"morada":["joao","allen"]}"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.