I am calling a 3rd Party API using Indy
var loRespJson: TMemoryStream;
IdHTTP1.GET(lsURL, loRespJson)
and it returns a JSON array:
[
{
"Active": "1",
"SourceId": "215",
"SourceName": "MyVal1"
},
{
"Active": "1",
"SourceId": "383",
"SourceName": "MyVal2"
}
]
In turn my function creates a new JSON object, adds additional info plus the response, and return it to the calling program. Desired result:
{
"responseCode":"200",
"companyNo":"0268895",
"responseMessage": [
{
"Active": "1",
"SourceId": "215",
"SourceName": "MyVal1"
},
{
"Active": "1",
"SourceId": "383",
"SourceName": "MyVal2"
}
]
}
How can I achieve the above? If I add using the following, it creates "" (quotes) around the array which is a big problem when parsing the JSON:
loJSon.AddPair(TJSONPair.Create('responseCode', IntToStr(idHttp1.ResponseCode)));
loJSon.AddPair(TJSONPair.Create('companyNo', CompanyNo));
if idHttp1.ResponseCode = 200 then
begin
lsRespMsg := StreamToString(loRespJSon);
liSuper := SO(lsRespMsg);
loJSon.AddPair(TJSONPair.Create('responseMessage', liSuper.AsJSon()));
…
I have also tried looping through the JSON aray but that option adds "" around each array item
{ create an json-array }
loJSA := TJsonArray.Create();
{ add array to object }
loJSP := TJSONPair.Create('responseMessage', loJSA);
loJSon.AddPair(loJSP);
if liSuper.IsType(stArray) then
begin
for i := 0 to liSuper.AsArray.Length - 1 do
begin
loSubscription := liSuper.AsArray[i];
loJSA.Add(loSubscription.AsJSon());
end;
end;
Any help will be greatly appreciated! Thanks.