You are not logged in.
I would like to consume the following JSON result from a REST service into a collection of Category objects. What would be the correct way to go about configuring Mormot to do just that?
{
"Categories": [
{
"CategoryId": 0,
"CategoryName": "Undefined",
"LastModified": "2016-01-15 08:00",
"Isactive": 1
},
{
"UnitCategoryId": 1,
"CategoryName": "Acceleration",
"LastModified": "2016-01-15 08:00",
"Isactive": 1
},
{
"UnitCategoryId": 2,
"CategoryName": "Angle",
"LastModified": "2016-01-15 08:00",
"Isactive": 1
},
{
"UnitCategoryId": 3,
"CategoryName": "Area",
"LastModified": "2016-01-15 08:00",
"Isactive": 1
}
]
}
I tried the following, (a bit dirty as I have been trying to sort this)
TUOM = class(TPersistent)
protected
fuomcid: string;
fuomcn: string;
published
property CategoryId: string read fuomcid write fuomcid;
property CategoryName: string read fuomcn write fuomcn;
end;
function TMUOMCategoryClient_REST1.Get: Boolean;
var
fuom: TUOM;
v: Boolean;
ll: TList;
l: TObjectList<TUOM>;
res, uomc: string;
cnt: integer;
cp: TJSONCustomParserCustom;
r: RawUTF8;
begin
// cp := TJSONCustomParserCustom.Create('UOMCategoryId', 'CategoryId');
fd := fclient.Get('http://localhost:3002/uomc', chdr);
r := RawUTF8(fd); //valid json here
l := TObjectList<TUOM>.Create(True);
Res := JSONToObject(l, @r[1], v, TUOM, []); //object list contains nothing
cnt := l.Count;
end;
Any assistance appreciated.
Last edited by Del (2017-03-24 14:41:03)
Offline
Thanks will resolve name for categoryid. I want to have my object's property names deviate from the rest response fields
How do I 'map' this? A simple example would be appreciated. Many thanks
Offline
Sorted this, had a couple of issues besides for what was mentioned (JSON issues) ... main one being using TObjectlist<> which is not supported. For custom mapping from JSON to object
TJSONSerializer.RegisterCustomSerializer specifying a reader function should be used, correct? Are there any samples available outlining use of such a scenario?
Thanks for an AWESOME Framework!:)
Offline