I am using a service to get a value from Id but receive. This value needs
to be stringify, parsed and retrieved to another iteration.
Is that code have an issue?
class Test {
static function getById(id:Int, list:Array<_Category>):_Category {
var obj:_Category = null;
for (c in list) {
if(c.id == id) { //error here
obj = c;
break;
}
}
return obj;
}
static function main() {
var collection = '[{"description":"tellus eu augue porttitor interdum. Sed auctor odio a purus.","id":1,"categoryId":5,"scname":"Jacob Pollard"},{"description":"risus. Donec egestas. Aliquam nec enim. Nunc ut erat. Sed","id":2,"categoryId":4,"scname":"Jana Grant"},{"description":"eu lacus. Quisque imperdiet, erat nonummy ultricies ornare, elit elit","id":3,"categoryId":3,"scname":"Catherine Floyd"},{"description":"neque sed sem egestas blandit. Nam nulla magna, malesuada vel,","id":4,"categoryId":2,"scname":"Ulla Burks"},{"description":"a neque. Nullam ut nisi a odio semper cursus. Integer","id":5,"categoryId":1,"scname":"Isadora Dalton"}]';
var list:Array<_Category> = haxe.Json.parse(collection);
try {
for(ca in list) {
var cstr = haxe.Json.stringify(ca);
var _cat = haxe.Json.parse(cstr);
trace(_cat.id != 0); //ok
}
var c = getById(1, list); // error
} catch (e:Dynamic) {
trace("Uncaught exception Unexpected value {description: tellus eu augue porttitor interdum. Sed auctor odio a purus., id: 1, categoryId: 5, scname: Jacob Pollard}, expected instance");
}
}
}
class _Category {
public var id:Int;
public var cname:String;
public var description:String;
public var cdate:String;
public var edate:String;
public function new() {
this.cdate = Date.now().toString();
this.edate = Date.now().toString();
}
}