Delphi TJSONArray.ToBytes Access violation - Is it
By : Mary Beel
Date : March 29 2020, 07:55 AM
This might help you Yes, your use of SizeOf() is wrong. iCJSONCallData is an object, so SizeOf(iCJSONCallData) is effectively the same as SizeOf(Pointer), which is 4 on 32bit and 8 on 64bit. You need to use the TJSONArray.EstimatedByteSize() method instead: code :
SetLength(vJSONCallDataBytes, iCJSONCallData.EstimatedByteSize);
vCount := iCJSONCallData.ToBytes(vJSONCallDataBytes, 0);
|
Find Value Type of a JSONValue (TJSONArray or TJSONObject)
By : haaapppy
Date : March 29 2020, 07:55 AM
Does that help I would like to do this with the standard Library in Delphi XE8 , You could use the is operator: code :
if Assigned(JSONValue) then
begin
if JSONValue is TJSONArray then
ProcessArrayResponse(TJSONArray(JSONValue))
else if JSONValue is TJSONObject then
ProcessObjectResponse(TJSONObject(JSONValue));
end;
type
JsonValueType = (jsArray, jsObject, ...);
function GetJsonValueType(JSONValue: TJSONValue): JsonValueType;
begin
if JSONValue is TJSONArray then Exit(jsArray);
if JSONValue is TJSONObjct then Exit(jsObject);
...
end;
...
if Assigned(JSONValue) then
begin
case GetJsonValueType(JSONValue) of
jsArray : ProcessArrayResponse(TJSONArray(JSONValue));
jsObject : ProcessObjectResponse(TJSONObject(JSONValue));
end;
end;
type
JsonValueType = (jsArray, jsObject, ...);
var
JsonValueTypes: TDictionary<String, JsonValueType>;
...
if Assigned(JSONValue) then
begin
case JsonValueTypes[JSONValue.ClassName] of
jsArray : ProcessArrayResponse(TJSONArray(JSONValue));
jsObject : ProcessObjectResponse(TJSONObject(JSONValue));
end;
end;
...
initialization
JsonValueTypes := TDictionary<String, JsonValueType>.Create;
JsonValueTypes.Add('TSONArray', jsArray);
JsonValueTypes.Add('TSONObject', jsObject);
...
finalization
JsonValueTypes.Free;
|
Delphi - How do I create a TJSONArray from a array of strings?
By : Colin Lim
Date : March 29 2020, 07:55 AM
I wish this help you You need to construct an empty TJSONArray object first, and then Add() the individual strings values to it. For example: code :
var
arr: array of string;
JSONObj: TJSONObject;
response_faults: TJSONArray;
I: Integer;
begin
arr := ... ; // '001', '002', '005', '009', ...
JSONObj := TJSONObject.Create;
try
response_faults := TJSONArray.Create;
try
for I := Low(arr) to High(arr) do begin
response_faults.Add(arr[I]);
end;
JSONObj.AddPair('Events', response_faults);
except
response_faults.Free;
raise;
end;
// use JSONObj as needed...
finally
JSONObj.Free;
end;
end;
|
Update TJSONArray
By : Danny Darwin
Date : March 29 2020, 07:55 AM
help you fix your problem Unfortunately, TJSONArray does not natively allow you to REPLACE existing elements with new values (Why? Who knows). All you can do is ADD and REMOVE elements, and ENUMERATE elements. Since your array holds TJSONObject instances, to replace such an element with a new object, you will have to either:
|
Do I need to free instance of TJSONArray
By : esz
Date : March 29 2020, 07:55 AM
With these it helps It depends on which platform you are running on. If your app is running on a non-ARC platform, such as Windows or OSX, then YES, you need to manually free the TJSONArray when you are done using it, or else it will be leaked.
|