Home > OS >  Delphi json array without bracket
Delphi json array without bracket

Time:10-18

{
      "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?

CodePudding user response:

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. You can loop through the child fields of the "data" object using TJSONObject (not TJSONArray!), 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