|
8 | 8 | % author: Qianqian Fang (q.fang <at> neu.edu) |
9 | 9 | % |
10 | 10 | % input: |
11 | | -% data: a hierachical data structure made of struct, containers.Map, dictionary, or cell arrays |
12 | | -% if data is a string starting with http:// or https://, |
13 | | -% loadjson(data) will be used to dynamically load the data |
| 11 | +% data: an array, or hierachical data structure made of struct, |
| 12 | +% containers.Map, dictionary, or cell arrays; if data is a |
| 13 | +% string starting with http:// or https://, loadjson(data) |
| 14 | +% will be used to dynamically load the data |
14 | 15 | % |
15 | 16 | % indexing: |
16 | | -% jd.('key1').('subkey1')... can retrieve values that are recursively index keys that are |
| 17 | +% jd.('key1').('subkey1')... can retrieve values that are recursively index keys |
17 | 18 | % jd.key1.subkey1... can also retrieve the same data regardless |
18 | 19 | % if the underlying data is struct, containers.Map or dictionary |
19 | 20 | % jd.('key1').('subkey1').v(1) if the subkey key1 is an array, this can retrieve the first element |
|
26 | 27 | % member functions: |
27 | 28 | % jd() or jd.v() returns the underlying hierachical data |
28 | 29 | % jd.('cell1').v(i) or jd.('array1').v(2:3) returns specified elements if the element is a cell or array |
29 | | -% jd.('key1'),('subkey1').v() returns the underlying hierachical data at the specified subkeys |
| 30 | +% jd.('key1').('subkey1').v() returns the underlying hierachical data at the specified subkeys |
30 | 31 | % jd.tojson() convers the underlying data to a JSON string |
31 | 32 | % jd.tojson('compression', 'zlib', ...) convers the data to a JSON string with savejson() options |
32 | 33 | % jd.keys() return the sub-key names of the object - if it a struct, dictionary or containers.Map - or 1:length(data) if it is an array |
33 | 34 | % jd.len() return the length of the sub-keys |
| 35 | +% jd.size() return the dimension vector |
34 | 36 | % jd{'attrname'} get/set attributes using curly brace indexing |
35 | 37 | % jd.setattr(jsonpath, attrname, value) set attribute at any path |
36 | 38 | % jd.getattr(jsonpath, attrname) get attribute from any path |
|
39 | 41 | % brackets .(...), but in octave, one must use .v(...) |
40 | 42 | % |
41 | 43 | % examples: |
42 | | -% obj = struct('key1', struct('subkey1',1, 'subkey2',[1,2,3]), 'subkey2', 'str'); |
43 | | -% obj.key1.subkey3 = {8,'test',containers.Map('subsubkey1',0)}; |
44 | 44 | % |
45 | | -% jd = jdict(obj); |
| 45 | +% jd = jdict; |
| 46 | +% jd.key1 = struct('subkey1',1, 'subkey2',[1,2,3]); |
| 47 | +% jd.key2 = 'str'; |
| 48 | +% jd.key1.subkey3 = {8,'test',containers.Map('special key',10)}; |
46 | 49 | % |
47 | 50 | % % getting values |
48 | 51 | % jd() % return obj |
49 | | -% jd.('key1').('subkey1') % return jdict(1) |
50 | | -% jd.keys.subkey1 % return jdict(1) |
51 | | -% jd.('key1').('subkey3') % return jdict(obj.key1.subkey3) |
52 | | -% jd.('key1').('subkey3')() % return obj.key1.subkey3 |
53 | | -% jd.('key1').('subkey3').v(1) % return jdict({8}) |
54 | | -% jd.('key1').('subkey3').('subsubkey1') % return jdict(obj.key1.subkey3(2)) |
55 | | -% jd.('key1').('subkey3').v(2).v() % return {'test'} |
| 52 | +% jd.key1.subkey1 % return jdict(1) |
| 53 | +% jd.('key1').('subkey1') % same as above |
| 54 | +% jd.key1.('subkey1') % same as above |
| 55 | +% jd.key1.subkey3 % return jdict(obj.key1.subkey3) |
| 56 | +% jd.key1.subkey3() % return obj.key1.subkey3 |
| 57 | +% jd.key1.subkey3.v(1) % return jdict(8) |
| 58 | +% jd.key1.subkey3.v(3).('special key') % return jdict(10) |
| 59 | +% jd.key1.subkey3.v(2).v() % return 'test' |
56 | 60 | % jd.('$.key1.subkey1') % return jdict(1) |
57 | 61 | % jd.('$.key1.subkey2')() % return 'str' |
58 | 62 | % jd.('$.key1.subkey2').v().v(1) % return jdict(1) |
|
62 | 66 | % jd.('$..subkey2').v(2) % return jdict([1,2,3]) |
63 | 67 | % |
64 | 68 | % % setting values |
65 | | -% jd.('subkey2') = 'newstr' % setting obj.subkey2 to 'newstr' |
66 | | -% jd.('key1').('subkey2').v(1) = 2; % modify indexed element |
67 | | -% jd.('key1').('subkey2').v([2, 3]) = [10, 11]; % modify multiple values |
68 | | -% jd.('key1').('subkey3').v(3).('subsubkey1') = 1; % modify keyed value |
69 | | -% jd.('key1').('subkey3').v(3).('subsubkey2') = 'new'; % add new key |
| 69 | +% jd.subkey2 = 'newstr' % setting obj.subkey2 to 'newstr' |
| 70 | +% jd.key1.subkey2.v(1) = 2; % modify indexed element |
| 71 | +% jd.key1.subkey2.v([2, 3]) = [10, 11]; % modify multiple values |
| 72 | +% jd.key1.subkey3.v(3).('special key') = 1; % modify keyed value |
| 73 | +% jd.key1.newkey = 'new'; % add new key |
70 | 74 | % |
71 | 75 | % % attributes |
72 | | -% jd{'dims'} = {'x','y','z'}; % set attribute |
73 | | -% %% {'attr'} is MATLAB-only |
74 | | -% jd.('a'){'dims'} = {'time','space'}; % set attribute on nested element(MATLAB only, Octave use .setattr()) |
75 | | -% %% use .setattr() in MATLAB/Octave |
76 | | -% jd.('a').setattr('dims',{'time','space'}); % set attributes |
77 | | -% jd.('a'){'dims'} % print attribute on nested element |
| 76 | +% jd.vol = zeros(2,3,4); % set 3d arrays |
| 77 | +% jd.vol{'dims'} = {'x','y','z'}; % set dimension labels (MATLAB-only) |
| 78 | +% jd.vol.setattr('dims', {'x','y','z'}); % set attribute in Octave |
| 79 | +% jd.vol{'dims'} % print dimension names |
| 80 | +% jd.vol{'units'} = 'mm'; % set any custom attributes |
| 81 | +% jd.vol.getattr('units') % retrieve attributes |
| 82 | +% jd.vol.getattr() % list all attribute paths |
78 | 83 | % |
79 | 84 | % % loading complex data from REST-API |
80 | | -% jd = jdict('https://neurojson.io:7777/cotilab/NeuroCaptain_2024'); |
| 85 | +% jd = jdict('https://neurojson.io:7777/cotilab/NeuroCaptain_2025'); |
81 | 86 | % |
82 | 87 | % jd.('Atlas_Age_19_0') |
83 | 88 | % jd.Atlas_Age_19_0.('Landmark_10_10').('$.._DataLink_') |
|
0 commit comments