I'm try to parse a object from response to web service, that has the following structure:
{ "idRonda":"1",
"puntos":{
"p1":{"id":1,"descripcion":"punto uno","tag":"0497e1b13e0280"},
"p2":{"id":2,"descripcion":"punto dos","tag":"0498e9b13e0280"},
"p4":{"id":4,"descripcion":"punto cuatro","tag":"04419092432f80"},
"p5":{"id":5,"descripcion":"punto cinco","tag":"0462f812b82a80"},
"p3":{"id":3,"descripcion":"punto tres","tag":"046cfd92432f80"}
}
}
So, I try to iterate over the "array" puntos, then I do the following:
//data has the response from web service
var json = JSON.parse(data);
var puntos = json.puntos; //until here it's ok
When I print the value and type of puntos :
console.log( " Puntos Object : "+ puntos );
console.log( " TypeOf : "+ Ember.typeOf(puntos) );
Output:
Puntos Object : [object Object]
TypeOf : string
My attempt:
for (var k in puntos)
{
// I thought that k must be "p1", "p2"..., but I'm wrong
if (puntos.hasOwnProperty(k))
{
var values = puntos[k];
var id = values['id'];
}
}
If I print the k value I get(I don't know what mean it):
key: 0
key: 1
.
.
.
key: 13
key: 14
key: fmt
key: w
key: loc
key: camelize
key: decamelize
key: dasherize
key: underscore
key: classify
key: capitalize
key: htmlSafe
Now how iterate over the puntos object to get id
, descripcion
and tag
values?
UPDATE:
var puntos = json.puntos;
var db = window.sqlitePlugin.openDatabase("Rondas", "1.0", "Dev", -1);
db.transaction(function(tx)
{
tx.executeSql("SELECT * from Punto;", [], function(tx, res)
{
if( res.rows.length === 0)
{
for (var k in puntos)
{
if (puntos.hasOwnProperty(k))
{
var id = puntos[k].id;
var descripcion = puntos[k].descripcion;
var tag = puntos[k].tag;
console.log( "KEY: "+k+" ,ID: "+id + " ,DESCRIPCIÓN: "+descripcion+" ,TAG: "+tag );
}
}
}
}
}
The code above fail becase the object puntos
lost the scope because it inside of a callback then then only solution is ensure the object context.
SOLUTION
db.transaction(function(tx)
{
//ensure the object context.
var points = puntos;
tx.executeSql("SELECT * from Punto;", [], function(tx, res)
{
if( res.rows.length === 0)
{
for (var k in points)
{
...
}
}
}
}