3
I am trying to pass a JSON to a Tclientdataset using the following function:
procedure JsonToDataset(aDataset : TDataSet; aJSON : string);
var
JObj: TJSONObject;
JArr: TJSONArray;
vConv : TCustomJSONDataSetAdapter;
i: Integer;
begin
if (aJSON = EmptyStr) then
begin
Exit;
end;
JArr := nil;
JObj := nil;
try
JArr := TJSONObject.ParseJSONValue(aJSON) as TJSONArray;
except
JObj := TJSONObject.ParseJSONValue(aJSON) as TJSONObject;
end;
vConv := TCustomJSONDataSetAdapter.Create(Nil);
try
vConv.Dataset := aDataset;
if JObj <> nil then
vConv.UpdateDataSet(JObj)
else
vConv.UpdateDataSet(JArr);
finally
vConv.Free;
JObj.Free;
end;
end;
However, when I have a large field, the function truncates my string in 255 characters. Example of JSON:
{
"string": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur rhoncus convallis risus, nec posuere nisl gravida vitae. Duis elementum augue nec condimentum rutrum. Aliquam sodales, dolor at laoreet pharetra, tortor eros efficitur eros, vel euismod sem nulla quis erat. Aliquam erat volutpat. Ut vitae congue lectus, et sodales velit. Cras suscipit pulvinar dolor ut consequat. Praesent eget pellentesque justo. In at maximus lectus, posuere mattis felis."
}
Is there any way around this problem?
In this case I would lose the dynamic I want with the idea, because I do not know what the name of the field that will come from REST. This way, I can’t create the field before.
– Cleber Griff
In the same way, friend, you will have to read the Json before converting and creating the fields dynamically! Because, it is a Class standard.
– Junior Moreira
When I do the
UpdateDataSet
it rebuilds Dataset. Is there any way to do it better? Even if you don’t use Tcustomjsondatasetadapter?– Cleber Griff
Ai would have to be "TOTALLY dynamic in the hand", that is, what I said earlier, go through the Json Object and recognize the keys, and then yes Create the Fields.
– Junior Moreira
I tried this way (http://collabedit.com/2e6pf), but after the Update, it erases and recreates my fields. I am doing something wrong?
– Cleber Griff
The way you tried it is valid, but the result you’ve seen, it erases. Without option, try to do what I told you before, create the fields dynamically based on JSON Keys
– Junior Moreira
Junior, could you give me a little example of how to generate the fields dynamically, please?
– Cleber Griff
I’m on mobile rsrsrs, get home edits Reply.
– Junior Moreira
I implemented the solution and the result came out as I needed! Thank you! But have some special reason pro Tcustomjsondatasetadapter do not automatically change the field to ftMemo?
– Cleber Griff
Does not have, it prefers ftWidString by default!
– Junior Moreira