#1 2025-01-03 16:06:00

mvg7
Member
Registered: 2021-10-03
Posts: 12

Parsing a Json Array

Hi! There are a lot of such topics in the forum, however Im still a bit confused what is the best (fastest) way to parse an array!

In mORMot 2 I have used such approach:

var
  x:                    Variant;
  symbols:           PDocVariantData;
begin
  DoRequest('https://fapi.binance.com/fapi/v1/exchangeInfo');
  obj := _Json(LastResponseA, JSON_OPTIONS_FASTMEM);
  symbols := DocVariantData(obj.symbols);

  for x in symbols.Values do sym := x.symbol;

However in mORMot 2 I have noticed in comments that length(Values)=Capacity so the code should look like

var
  x:                    PVariant;
  symbols:           PDocVariantData;
begin
  symbols := DocVariantData(obj.symbols);

  for x in symbols.Items do sym := x^.symbol;

In this version we should use pointer to Variant, is it correct?
In other topics its recommened to use the _Safe procedure, however I didnt yet understand the reason... could you please clarify! Thanks !

Offline

#2 2025-01-03 16:42:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,793
Website

Re: Parsing a Json Array

The easiest is to NOT use variants, but TDocVariantData directly for x.

Or even easier, use a IDocDict instance.

Offline

#3 2025-01-03 21:56:34

mvg7
Member
Registered: 2021-10-03
Posts: 12

Re: Parsing a Json Array

ab wrote:

The easiest is to NOT use variants, but TDocVariantData directly for x.

Or even easier, use a IDocDict instance.

Second method looks like this?

var x: Variant;
for x in DocList(_Safe(obj.symbols)^) do ;

First method I didnt understand, could you please give a hint... If I use

symbols := _Safe(obj.symbols);

then to iterate using .Items Enumerator,  I need a PVariant?

Offline

Board footer

Powered by FluxBB