ISuperObject.AsObject 可获取一个 TSuperTableString 对象.
TSuperTableString 的常用属性: count、GetNames、GetValues
var jo: ISuperObject; jts: TSuperTableString; begin jo := SO(‘{A:1, B:2, C:3, D:{x:4, y:5, z:6}}‘); jts := jo.AsObject; ShowMessage(IntToStr(jts.count)); // 4 ShowMessage(jts.GetNames.AsString); // ["D","C","B","A"] ShowMessage(jts.GetValues.AsString); // [{"z":6,"y":5,"x":4},3,2,1] jts := jo[‘D‘].AsObject; ShowMessage(IntToStr(jts.count)); // 3 ShowMessage(jts.GetNames.AsString); // ["z","y","x"] ShowMessage(jts.GetValues.AsString); // [6,5,4] end;
使用FOR...IN...遍历子对象,不适用于DELPHI7等低版本,因为它们不支持此语法。
var jo,i: ISuperObject; begin jo := SO(‘{A:1, B:2, C:3, D:[4, 5, {X:6}, {Y:[7,8,{m:9}]}]}‘); for i in jo do ShowMessage(i.AsString); end; TSuperAvlIterator 枚举出的是 TSuperAvlEntry 对象(其包含 Name 和 Value);获取 TSuperAvlIterator 方法:TSuperTableString.GetEnumerator 或者 ISuperObject.AsObject.GetEnumerator.
var jo: ISuperObject; ite: TSuperAvlIterator; begin jo := SO(‘{A:1, B:2, C:3, D:[4, 5, {X:6}, {Y:7}]}‘); ite := jo.AsObject.GetEnumerator;
ite.First;
while ite.MoveNext do
begin
lname := ite.Current.Name;
lvalue := ite.Current.Value.AsString;
ite.Next;
end;
end;
ObjectFindFirst、ObjectFindNext、ObjectFindClose 是 SuperObject 给的另一种枚举元素的方式.
它用到了一个结构:
TSuperObjectIter = record key: SOString; val: ISuperObject; Ite: TSuperAvlIterator; end; //应用举例: var jo: ISuperObject; iter: TSuperObjectIter; begin jo := SO(‘{A:1, B:2, C:3, D:[4, 5, {X:6}, {Y:7}]}‘); if ObjectFindFirst(jo, iter) then begin repeat ShowMessageFmt(‘%s - %s‘, [iter.key, iter.val.AsString]); until not ObjectFindNext(iter); end; ObjectFindClose(iter); end; //或者: var jo: ISuperObject; iter: TSuperObjectIter; begin jo := SO(‘{A:1, B:2, C:3, D:[4, 5, {X:6}, {Y:7}]}‘); if ObjectFindFirst(jo, iter) then begin repeat ShowMessageFmt(‘%s - %s‘, [iter.Ite.Current.Name, iter.Ite.Current.Value.AsString]); until not ObjectFindNext(iter); end; ObjectFindClose(iter); end;
时间: 2024-10-12 08:50:54