#1 2016-02-25 06:46:21

chowong
Member
Registered: 2016-02-04
Posts: 4

How to return data as True JSON not String (Browser + mORMot Service )

Hi Everyone

     I have some issue about JSON data returned from mORMot Service.

I have created a service with mORMot (TCarService.GetData) and

the service is called from Browser Client (AngularJS).

Below is service code 

function TCarService.GetData(aSqlText: RawUTF8): RawUTF8;
var
  Props : TSQLDBConnectionProperties;
  res : ISQLDBRows;
begin

  Props := TOleDBMSSQL2012ConnectionProperties.Create('127.0.0.1', 'Car_DB', 'sa', 'password');

  try
    res := Props.ExecuteInlined(aSqlText, True);

    result := res.FetchAllAsJSON(True);
  finally
    Props.Free;
  end;
end;

but the returned JSON result looks like this

{"result":["[
  {\"MakeCode\":\"ALFA\"},
  {\"MakeCode\":\"ASTO\"},
  {\"MakeCode\":\"AUDI\"},
  {\"MakeCode\":\"BENT\"},
  {\"MakeCode\":\"BMW \"},
  {\"MakeCode\":\"CHER\"},
  {\"MakeCode\":\"TOYO\"},
  {\"MakeCode\":\"VOLK\"},
  {\"MakeCode\":\"VOLV\"}
]\n"]}

Is there ways to return as True JSON data not string (with FetchAllAsJSON(True)) ?

or I must remove '"', '\' , '\n'  at Client Side.

expected Result like this

{"result":
  [
    {"MakeCode":"ALFA"},
    {"MakeCode":"ASTO"},
    {"MakeCode":"AUDI"},
    {"MakeCode":"BENT"},
    {"MakeCode":"BMW"},
    {"MakeCode":"CHER"},
    {"MakeCode":"TOYO"},
    {"MakeCode":"VOLK"},
    {"MakeCode":"VOLV"}
]}

Thank you very much

Offline

#2 2016-02-25 13:04:30

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to return data as True JSON not String (Browser + mORMot Service )

Your method returns a RawUTF8, so it returns a JSON string.

Use RawJSON to return some JSON content, without conversion to string.

See http://synopse.info/files/html/Synopse% … l#TITL_154

Offline

#3 2016-02-26 01:40:11

chowong
Member
Registered: 2016-02-04
Posts: 4

Re: How to return data as True JSON not String (Browser + mORMot Service )

That works Great! Thank you very much  ab. smile

Offline

Board footer

Powered by FluxBB