exhtml
2/16/2018 - 6:53 AM

Map JSON data to a class/object in JavaScript


// Example 1
// *requires you to have the same properties in your object and your JSON object.

var fra = {
	"ipda": 6786868,
	"eventType": 1,
	"description": "Recogido",
	"lat": 39.4699075,
	"lon": -0.3762881
};

function fru(ipda, eventType, description, lat, lon) {
    this.ipda = ipda;
    this.eventType = eventType;
    this.description = description;
    this.other = "Fixed value";
    this.coordinates = [lon, lat];
    this.kk1 = lon; // undefined por que?
    this.kk2 = lat; // undefined por que?
}

var fro = $.extend(new fru(), fra);

console.log(fro);