Json treatment

2

I have the following JSON return from a page:

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

    
asked by anonymous 22.06.2018 / 21:31

2 answers

2

You 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 get the values.

Another way is to use for normal using as 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);
}
    
22.06.2018 / 21:40
1

You can use $ each as well. Something 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 );
    }
  });
});
    
22.06.2018 / 21:56