Delphi json array without bracket

Viewed 209
{
      "code": 0,
      "data": {
        "KAVAUSDT": {
          "name": "KAVAUSDT",
          "min_amount": "0.5",
          "maker_fee_rate": "0.003",
          "taker_fee_rate": "0.003",
          "pricing_name": "USDT",
          "pricing_decimal": 4,
          "trading_name": "KAVA",
          "trading_decimal": 8
        },
        "CFXUSDT": {
          "name": "CFXUSDT",
          "min_amount": "5",
          "maker_fee_rate": "0.003",
          "taker_fee_rate": "0.003",
          "pricing_name": "USDT",
          "pricing_decimal": 6,
          "trading_name": "CFX",
          "trading_decimal": 8
        },
        ... continue 
      }
    }

If there were [ and ] symbols, I could solve it quickly with TJsonArray:

...

JsonArray := JsonValue.GetValue<TJSONArray>('data');

for ArrayElement in JsonArray do
begin
  tempName           := ArrayElement.GetValue<String>('name');
  tempPricingName    := ArrayElement.GetValue<String>('pricing_name');   
  ...
end;

There are no [and ] symbols in this Json type.

Without the [ and ] symbols, I cannot access the data, as it is using a for loop.

Is there a simple solution?

1 Answers

There is no array in the JSON document you have shown. "KAVAUSDT", "CFXUSDT", etc are not array elements, they are simply named object fields of the "data" object. If you need to loop through the child fields of the "data" object, you can use TJSONObject (not TJSONArray!) for that, eg:

...

JsonObj := JsonValue.GetValue<TJSONObject>('data');

for Field in JsonObj do
begin
  FieldObj           := Field.JsonValue as TJSONObject;
  tempName           := FieldObj.GetValue<String>('name');
  tempPricingName    := FieldObj.GetValue<String>('pricing_name');   
  ...
end;
Related