You are not logged in.
Pages: 1
Subject : JSON record array on Android
Hello All,
I have been able to manipulate JSON array of records on Windows.
However, on Android I can not generate the array of records from the JSON.
This pseudo code works on Windows :-
TMyRecord = record
CompanyId : String;
ClientId : String;
end;
TRecArray = record
MyRecArray : array of TMyRecord;
end;
Rec1 : TMyRecord;
Rec2 : TMyRecord;
JsonStr : String;
RecArray : TRecArray;
begin
Rec1.CompanyId := 'Company1';
Rec1.ClientId := 'Client1';
Rec2.CompanyId := 'Company2';
Rec2.ClientId := 'Client2';
JsonStr := '{"MyRecArray":[';
JsonStr := JsonStr +
SynCommons.RecordSaveJSON(
Rec1,
TypeInfo(TMyRecord));
JsonStr := JsonStr + ',' +
SynCommons.RecordSaveJSON(
Rec2,
TypeInfo(TMyRecord));
JsonStr := JsonStr + ']}';
SetLength(RecArray.MyRecArray, 2);
bResult := SynCommons.RecordLoadJSON(
RecArray,
JsonStr,
TypeInfo(TRecArray));
end;
On Android I can get the JSON using :-
var
RecVariant : Variant;
begin
JsonStr := '{"MyRecArray":[';
RecVariant := TMyRecord2Variant(Rec1);
JsonStr := JsonStr +
SynCrossPlatformJSON.ValueToJSON(RecVariant);
RecVariant := TMyRecord2Variant(Rec2);
JsonStr := JsonStr + ',' +
SynCrossPlatformJSON.ValueToJSON(RecVariant);
JsonStr := JsonStr + ']}';
{The JsonStr is the same as on Windows}
end;
Now on Android generating the array of records (This does not work) :-
begin
RecVariant := SynCrossPlatformJSON.JSONToValue(JsonStr);
RecArray := Variant2MyRecordArray(RecVariant, 2); {Error in this call}
end;
function Variant2MyRecordArray(var aRecVariant : Variant;
const aRecsInArray : Integer) : TRecArray;
var
iIterate : Integer;
Str : String;
begin
if aRecsInArray <= 0 then begin
SetLength(Result.MyRecArray, 0);
Exit;
end;
SetLength(Result.MyRecArray, aRecsInArray);
for iIterate := 0 to (aRecsInArray - 1) do begin
Str := aRecVariant.MyRecArray[iIterate].CompanyId; {Error - Variant method calls not supported}
Result.MyRecArray[iIterate].CompanyId := Str;
...
end;
end;
This last part in Android doesn't work.
Perhaps there is a simpler way to achieve this on Android?
Any ideas?
Regards,
Peter G. Evans
Offline
The JSON is this :-
{"MyRecArray":[{"CompanyId":"Company1","ClientId":"Client1"},{"CompanyId":"Company2","ClientId":"Client1"}]}
This JSON validates at the website JSONLint.
Offline
I now have a solution for Android. This is :-
var
RecsInArray : Integer;
iLen : Integer;
KindJSON : SynCrossPlatformJSON.TJSONVariantKind;
RecVariantData : SynCrossPlatformJSON.TJSONVariantData;
begin
RecsInArray := 2;
{Need to pass an array.
So remove leading characters. Do not worry about trailing bracket}
iLen := Length('{"MyRecArray":');
{ your routine here to remove leading characters from JSONStr }
RecVariantData.Init(JSONStr);
KindJSON := RecVariantData.Kind;
if KindJSON <> jvArray then
Exit;
if RecVariantData.Count <> RecsInArray then
Exit;
SetLength(RecArray.MyRecArray, RecsInArray);
RecArray.MyRecArray[0] := Variant2MyRecord(RecVariantData.Item[0]);
RecOut1 := RecArray.MyRecArray[0]; {test}
RecArray.MyRecArray[1] := Variant2MyRecord(RecVariantData.Item[1]);
RecOut2 := RecArray.MyRecArray[1]; {test}
end;
Offline
Validate it here: json parser
Offline
Pages: 1