Handling of json in Jquery

Asked

Viewed 76 times

2

I have the following one-page JSON return:

{  
   "0":{  
      "id_user":"10",
      "contente":"45454545454",
      "time":"azul",
      "nick":"ikeda."
   },
   "1":{  
      "id_user":"10",
      "contente":"4",
      "time":"azul",
      "nick":"ikeda."
   },
   "2":{  
      "id_user":"10",
      "contente":"5",
      "time":"azul",
      "nick":"ikeda."
   },
   "3":{  
      "id_user":"10",
      "contente":"45",
      "time":"azul",
      "nick":"ikeda."
   },
   "4":{  
      "id_user":"10",
      "contente":"44",
      "time":"azul",
      "nick":"ikeda."
   },
   "status":"success",
   "msgs":5
}

They are dynamic returns, that is, sometimes they can come 1 or times 20, so I need to be able to take all the indices of each "array", and take the data of the "sub-arrays"

  • It will always come in sequence 0, 1, 2...?

  • yes. always in that sequence

2 answers

2


Can use for...in to iterate the object:

var json ={
   "0":{  
      "id_user":"10",
      "contente":"45454545454",
      "time":"azul",
      "nick":"ikeda."
   },
   "1":{  
      "id_user":"10",
      "contente":"4",
      "time":"azul",
      "nick":"ikeda."
   },
   "2":{  
      "id_user":"10",
      "contente":"5",
      "time":"azul",
      "nick":"ikeda."
   },
   "3":{  
      "id_user":"10",
      "contente":"45",
      "time":"azul",
      "nick":"ikeda."
   },
   "4":{  
      "id_user":"10",
      "contente":"44",
      "time":"azul",
      "nick":"ikeda."
   },
   "status":"success",
   "msgs":5
}


for(var item in json){
   console.log(json[item]); // pega os objetos
   for(var val in json[item]){
      console.log(json[item][val]); // pega os valores
   }
}

The above example takes the objects and their values in two loops. Now it will depend on how you want to take the values.

Another way is by using for normal using the maximum value of the loop the value in the key msgs:

var json ={
   "0":{  
      "id_user":"10",
      "contente":"45454545454",
      "time":"azul",
      "nick":"ikeda."
   },
   "1":{  
      "id_user":"10",
      "contente":"4",
      "time":"azul",
      "nick":"ikeda."
   },
   "2":{  
      "id_user":"10",
      "contente":"5",
      "time":"azul",
      "nick":"ikeda."
   },
   "3":{  
      "id_user":"10",
      "contente":"45",
      "time":"azul",
      "nick":"ikeda."
   },
   "4":{  
      "id_user":"10",
      "contente":"44",
      "time":"azul",
      "nick":"ikeda."
   },
   "status":"success",
   "msgs":5
}


for(var x=0; x<json.msgs; x++){
   console.log(json[x]); // pega os objetos
   console.log(json[x].id_user);
   console.log(json[x].contente);
   console.log(json[x].time);
   console.log(json[x].nick);
}

  • Thank you. A with the traditional for worked very well. Good afternoon.

1

You have the possibility to use $each as well. Something more or less like this:

var data = [ 
{  
   "0":{  
      "id_user":"10",
      "contente":"45454545454",
      "time":"azul",
      "nick":"ikeda."
   },
   "1":{  
      "id_user":"10",
      "contente":"4",
      "time":"azul",
      "nick":"ikeda."
   },
   "2":{  
      "id_user":"10",
      "contente":"5",
      "time":"azul",
      "nick":"ikeda."
   },
   "3":{  
      "id_user":"10",
      "contente":"45",
      "time":"azul",
      "nick":"ikeda."
   },
   "4":{  
      "id_user":"10",
      "contente":"44",
      "time":"azul",
      "nick":"ikeda."
   },
   "status":"success",
   "msgs":5
}
];


$.each(data, function(a, item) {
  $.each(item, function(i, subitem) {
    if( i > 0 ){
      alert(
        "indice: " + i
        + ", " + 
        "id_user: " + subitem.id_user
        + ", " + 
        "contente: " + subitem.contente
        + ", " + 
        "time: " + subitem.time
        + ", " + 
        "nick: " + subitem.nick );
    }
  });
});

Browser other questions tagged

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