1
I have an API in php that returns data like this:
{"status":"sucesso","dados":[{"id":"1","nome":"Nome do Usuario","etc..":"etc.."},{"id":"2","nome":"Nome do Usuario","etc..":"etc.."}]}
I need to read and write 2 of all data (id and name) in an object, but I didn’t understand very well the part of how to write the data, so far I have it:
//Pesquisa o Json
$.getJSON('http://192.168.0.106/teste/SimpleBook/api/v1/Mostrar/Usuarios/', function(data) {
if (!data.error) {
var geraldata = data['dados'];
var data = [];
for (var i = geraldata.length - 1; i >= 0; i--) {
data.push ({
id: data['dados'][i]['id'],
text: data['dados'][i]['name']
});
}
} else {
alert(data.msg)
}
});
The expected result would be something like this:
// Resultado esperado
var data = [{
id: 0,
text: 'Nome do Usuario'
}, {
id: 1,
text: 'Nome do Usuario'
}];
The format of your return is not valid!
– LeAndrade
What would be the correct format for return?
– Alisson Custodio
In the format you entered in the question this coming object inside the object { {"id": "01" .... and this is not a valid syntax, the right one would be an array and inside it the objects.
– LeAndrade
Ahnn yes sorry, I just corrected the example.. This part validates, my difficulty is in writing the same data
– Alisson Custodio
And you want to write where? Database, file, etc. In your script you are already reading (note the name of the attribute "name" which is like "name")
– Tiago Sabadini
I only need to record the return in a variable
– Alisson Custodio