How to parse JSON items, and then display the results using the TListView component?

178 Views Asked by At

Here is the Json data :

{
  "meta": {
      "code": 200,
      "disclaimer": "Usage subject to terms"
  },
  "response": {
      "date": "2022-09-16T01:59:00Z",
      "base": "USD",
      "rates": {
          "ADA": 2.13250314,
          "ZMW": 15.57878043,
          "ZWD": 361.9,
          "ZWL": 604.11755006
      }
  }
}

I want to parse the rates object using the TJSONIterator.Next, TJSONIterator.Recurse, and TJSONIterator.Return methods, and display the retrieved data using the TListView.

But the code I wrote shows me only one item inside rates, and on the other hand when I do it with the Memo all items are displayed.

ListView1.Items.Clear;
ListView1.Items.BeginUpdate;
try
  oItem := ListView1.Items.Add;
  try
    while True do
    begin
      while LIterator.Next do
        if LIterator.&Type in [TJsonToken.StartObject, TJsonToken.StartArray] then
        begin                                                // Iterate into Rates object and display all the Ccies + Values
          LIterator.Recurse;
          LIterator.Next('rates');
          LIterator.Recurse;
          while LIterator.Next do
          begin
            LIterator.Recurse;
            oItem.Text := LIterator.Key {+': '+ LIterator.AsValue.ToString};
          end;
        end;
      if LIterator.InRecurse then
        LIterator.Return
      else
        Break;
    end;
  finally
    LIterator.Free;
  end;
finally
  ListView1.Items.EndUpdate;
end;
0

There are 0 best solutions below