Advertisement
danieleteti

#DMVCFramework Deserialize a JSONArray into a list of object

Dec 10th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 0.87 KB | None | 0 0
  1. //the HTTP request body contains
  2. [
  3.   {"code":"C342","description":"PIZZA CARBONARA XXL","price":8},
  4.   {"code":"C324","description":"PIZZA PEPPERONI, MUSHROOMS AND CHEESE","price":12},
  5.   {"code":"C333","description":"4 SEASONS PIZZA","price":9}
  6. ]
  7.  
  8.  
  9. //in the controller declaration
  10.     [MVCDoc('Creates new articles from a list and returns "201: Created"')]
  11.     [MVCPath('/bulk')]
  12.     [MVCHTTPMethod([httpPOST])]
  13.     procedure CreateArticles(Context: TWebContext);
  14.  
  15. //in the implementation section
  16. procedure TArticlesController.CreateArticles(Context: TWebContext);
  17. var
  18.   lArticles: TObjectList<TArticle>;
  19.   lArticle: TArticle;
  20. begin
  21.   lArticles := Context.Request.BodyAsListOf<TArticle>;
  22.   try
  23.     for lArticle in lArticles do
  24.     begin
  25.       GetArticlesService.Add(lArticle);
  26.     end;
  27.     Render(201, 'Articles Created');
  28.   finally
  29.     lArticles.Free;
  30.   end;
  31. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement