Data sets and JSON convert each other

Source: Internet
Author: User

Using Delphi native classes to convert datasets and JSON to each other

JSON two features: Arrays and objects. An object can contain an array, and an array can contain objects. No layer limit. Olevariant is similar, an element of olevariant can be olevariant, and there is no limit to the number of layers. XML is also similar. This is where they can sequence all the objects of the AO seconds.
JSON is a string, the string format after the definition of the sequence, and even do not need any JSON class library, you can assemble the JSON string and parse JSON strings to get the data.

USES System.json

1) Convert the dataset to a JSON string:

Function Tform1.datasettojson (adataset:tdataset): string;
//[{"Cityid": "CityName": "Xi ' an"},{"Cityid": "The", "CityName": "Guangzhou"}]
var
lrecord:string;
Lfield:tfield;
I:integer;
Begin
Result: = ';
if (not adataset.active) or (adataset.isempty) then
Exit;
Result: = ' [';
Adataset.disablecontrols;
Adataset.first;
While not adataset.eof does
begin
for I: = 0 to Adataset.fieldcount-1 do
begin
Lfield: = Adataset.fie Lds[i];
If Lrecord = "then
Lrecord: = ' {" ' + Lfield.fieldname + ' ":" ' + Lfield.text + ' "'
Else
Lrecord: = Lreco Rd + ', "' + Lfield.fieldname + '": "' + Lfield.text + '";
If i = Adataset.fieldcount-1 then
begin
Lrecord: = Lrecord + '} ';
If result = ' "then
Result: = Re Sult + Lrecord
Else
Result: = result + ', ' + Lrecord;
Lrecord: = ";
End;
End;
Adataset.next;
End;
Adataset.enablecontrols;
Result: = result + '] ';
End;

2) Convert the JSON string to a dataset:

Procedure Tform1.jsontodataset (ajson:string; Adataset:tdataset);
Var
Jdataset:tjsonarray;
Jrecord:tjsonobject;
I, J:integer;
Begin
if (Ajson = ") or (Adataset = nil) or (not adataset.active) then
Exit;
Jdataset: = TJSONObject.Create.ParseJSONValue (Ajson, True) as Tjsonarray;
While not adataset.eof do
Adataset.delete;
For I: = 0 to Jdataset.size-1 do
Begin
Adataset.append;
Jrecord: = Jdataset.get (i) as tjsonobject;
For J: = 0 to Adataset.fieldcount-1 do
ADATASET.FIELDS[J]. Text: = Jrecord.getvalue (Adataset.fields[j]. FieldName). ToString;
Adataset.post;
End
End

Data sets and JSON convert each other

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.