#1 2024-11-16 09:46:45

vetal71
Member
Registered: 2024-11-16
Posts: 4

Help for uses IDocList

Hello
I need help.
I have the next json

[
    {
        "id": "f7518487-6e95-4c90-8438-a6b48d6a8b5f",
        "name": "user",
        "phones": [
			{
				"type": "mobile"
				"number": "123-456-789"
			}
			
		]
    }
]

I need to get the value of nested array phones[0].number
How can i do this via IDocList?

PS. Sorry for my English

Offline

#2 2024-11-17 01:41:31

zen010101
Member
Registered: 2024-06-15
Posts: 66

Re: Help for uses IDocList

var
  d1, d2: IDocDict;
  l: IDocList;
begin
  l := DocList(j);
  for d1 in l do
    for d2 in d1.L['phones'] do
      writeln(d2['number']);
end;    

Offline

#3 2024-11-17 16:44:55

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

Re: Help for uses IDocList

This code works on Delphi but not on FPC, due to a FPC bug.

On FPC:

var
  d1, d2: IDocDict;
  l1, l2: IDocList;
begin
  l1 := DocList(j);
  for d1 in l1 do
  begin
    l2 := d1.L['phones'];
    for d2 in l2 do
      writeln(d2['number']);
  end;
end;    

See https://github.com/synopse/mORMot2/issues/322

Offline

#4 2024-11-18 07:38:02

vetal71
Member
Registered: 2024-11-16
Posts: 4

Re: Help for uses IDocList

I use delphi XE, mORMot 2.3 stable
and get compiler error "[DCC Error] TestApp_FMain.pas(1645): E2010 Incompatible types: 'Integer' and 'string'

for d1 in DocList(json) do
    for d2 in d1.L['phones'] do // Exception is here
      writeln(d2['number']);

Offline

#5 2024-11-18 07:45:11

vetal71
Member
Registered: 2024-11-16
Posts: 4

Re: Help for uses IDocList

/// access one element in the list, as IDocList (List)
// - warning: weak reference to the main list, unless you explicitly Copy it
property L[position: integer]: IDocList
   read GetL write SetL;

what means "position" in this context?

Offline

#6 2024-11-18 07:52:54

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

Re: Help for uses IDocList

"position" in the list, i.e. index of a value (0..Count-1) in IDocList.

Offline

#7 2024-11-18 10:06:08

vetal71
Member
Registered: 2024-11-16
Posts: 4

Re: Help for uses IDocList

thanks for everyone
all works after change IDocList to IDocDict

Offline

Board footer

Powered by FluxBB