Delphi Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Let's look at the following steps:

  1. Create a new VCL application and drop in three TButton and a TMemo. Align all the buttons as a toolbar at the top of the form and the memo to all the remaining from client areas.
  1. From left to right, name the buttons btnGenerateJSON, btnModifyJSON, and btnParseJSON.
  2. We'll use static data as our data source. A simple matrix is enough for this recipe. Just after the start of the implementation section of the unit, write the following code:
type 
  TCarInfo = ( 
    Manufacturer = 1, 
    Name = 2, 
    Currency = 3, 
    Price = 4); 
 
var 
  Cars: array [1 .. 4] of 
array [Manufacturer .. Price] of string = ( ('Ferrari','360 Modena','EUR', '250000'), ('Ford', 'Mustang', 'USD', '80000'), ('Lamborghini', 'Countach', 'EUR','300000'), ('Chevrolet', 'Corvette', 'USD', '100000') );
  1. TMemo is used to show our JSON files and our data. To keep things clear, create a public property called JSON on the form and map its setter and getter to the Memo1.Lines.Text property. Use the following code:
//...other form methods declaration  
private 
  procedure SetJSON(const Value: String); 
  function GetJSON: String; 
public 
  property JSON: String read GetJSON write SetJSON; 
end; 
 
//...then in the implementation section 
function TMainForm.GetJSON: String; 
begin 
  Result := Memo1.Lines.Text; 
end; 
 
procedure TMainForm.SetJSON(const Value: String); 
begin 
  Memo1.Lines.Text := Value; 
end; 

  1. Now, create event handlers for each button and write the code that follows. Pay attention to the event names:
procedure TMainForm.btnGenerateJSONClick(Sender: TObject); 
var 
  i: Integer; 
  JSONCars: TJSONArray; 
  Car, Price: TJSONObject; 
begin 
  JSONCars := TJSONArray.Create; 
  try 
    for i := Low(Cars) to High(Cars) do 
    begin 
      Car := TJSONObject.Create; 
      JSONCars.AddElement(Car); 
      Car.AddPair('manufacturer', 
Cars[i][TCarInfo.Manufacturer]); Car.AddPair('name', Cars[i][TCarInfo.Name]); Price := TJSONObject.Create; Car.AddPair('price', Price); Price.AddPair('value',
TJSONNumber.Create(
Cars[i][TCarInfo.Price].ToInteger)); Price.AddPair('currency',
Cars[i][TCarInfo.Currency]); end; JSON := JSONCars.ToJSON; finally JSONCars.Free; end; end; procedure TMainForm.btnModifyJSONClick(Sender: TObject); var JSONCars: TJSONArray; Car, Price: TJSONObject; begin JSONCars := TJSONObject.ParseJSONValue(JSON) as TJSONArray; try Car := TJSONObject.Create; JSONCars.AddElement(Car); Car.AddPair('manufacturer', 'Hennessey'); Car.AddPair('name', 'Venom GT'); Price := TJSONObject.Create; Car.AddPair('price', Price); Price.AddPair('value', TJSONNumber.Create(600000)); Price.AddPair('currency', 'USD'); JSON := JSONCars.ToJSON; finally JSONCars.Free; end; end; procedure TMainForm.btnParseJSONClick(Sender: TObject); var JSONCars: TJSONArray; i: Integer; Car, JSONPrice: TJSONObject; CarPrice: Double; s, CarName, CarManufacturer, CarCurrencyType: string; begin s := ''; JSONCars := TJSONObject.ParseJSONValue(JSON) as TJSONArray; if not Assigned(JSONCars) then raise Exception.Create('Not a valid JSON'); try for i := 0 to JSONCars.Count - 1 do begin Car := JSONCars.Items[i] as TJSONObject; CarName := Car.GetValue('name').Value; CarManufacturer :=
Car.GetValue('manufacturer').Value; JSONPrice := Car.GetValue('price') as TJSONObject; CarPrice := (JSONPrice.GetValue('value') as
TJSONNumber).AsDouble; CarCurrencyType := JSONPrice.GetValue('currency') .Value s := s + Format( 'Name = %s' + sLineBreak + 'Manufacturer = %s' + sLineBreak + 'Price = %.0n%s' + sLineBreak + '-----' + sLineBreak, [CarName, CarManufacturer, CarPrice, CarCurrencyType]); end; JSON := s; finally JSONCars.Free; end; end;
  1. Run the application by hitting F9 (or by going to Run | Run).
  2. Click on the btnGenerateJSON button, and you should see a JSON array and some JSON objects in the memo.
  3. Click on the btnModifyJSON button, and you should see one more JSON object inside the outer JSON array in the memo.
  4. Click on the last button, and you should see the same data as before, but with a normal text representation.
  5. After the third click, you should see something similar to the following screenshot:
Figure 1.13: Text representation of the JSON data generated and modified