VBA巢狀字典的遞迴輸出(序列化)

2021-09-13 01:58:12 字數 4028 閱讀 2018

vba巢狀字典的遞迴輸出(序列化)

開發平台:vbe

平台版本上限:未知

平台版本下限:未知

開發語言:vba

簡介: 用遞迴的方法將乙個vba的巢狀字典物件全部輸出到文字框內。

最近筆者的工作中使用到了vba的dictionary(字典)物件,這種物件是一種鍵值對物件,表現形式為:key:item ,其中key是不可重複的,item也可以為另外乙個字典,多個字典巢狀所形成的物件可以讓我很方便地操作乙個類,我在這乙個物件內完成大部分的動作,極大地簡化了我的**。

要建立這樣的物件,只需要像下面這樣做就行了:

dim alldic as object

dim peopledic as object

dim housesdic as object

dim housedic as object

dim rommdic as object

set alldic = createobject("scripting.dictionary")

set peopledic = createobject("scripting.dictionary")

set housesdic = createobject("scripting.dictionary")

set housedic = createobject("scripting.dictionary")

set rommdic = createobject("scripting.dictionary")

rommdic.add 1,"客廳"

rommdic.add 2,"主臥"

rommdic.add 3,"廚房"

housedic.add "addr","中山路3號"

housedic.add "price","120萬"

housedic.add "rooms",rommdic

housesdic.add 1,housedic

rommdic.removeall

housedic.removeall

rommdic.add 1,"客廳"

rommdic.add 2,"主臥"

rommdic.add 3,"陽台"

housedic.add "addr","西安路58號"

housedic.add "price","90萬"

housedic.add "rooms",rommdic

housesdic.add 2,housedic

peopledic.add "name","王明"

peopledic.add "birthdate","1990-01-01"

peopledic.add "horses",housesdic

alldic.add 1,peopledic

rommdic.removeall

housedic.removeall

housesdic.removeall

peopledic.removeall

rommdic.add 1,"客廳"

rommdic.add 2,"主臥"

rommdic.add 3,"次臥1"

housedic.add "addr","北京路159號"

housedic.add "price","145萬"

housedic.add "rooms",rommdic

housesdic.add 1,housedic

peopledic.add "name","李紅"

peopledic.add "birthdate","1980-10-01"

peopledic.add "horses",housesdic

alldic.add 2,peopledic

這樣我們建立了alldic這樣乙個巢狀的字典,他的實際內容是這樣的:

alldic:

1:name:"王明"

birthdate:"1990-01-01"

horses:

1:addr:"中山路3號"

price:"120萬"

rooms:

1:"客廳"

2:"主臥"

3,"廚房"

2:addr:"西安路58號"

price:"90萬"

rooms:

1:"客廳"

2:"主臥"

3,"陽台"

2:name:"李紅"

birthdate:"1980-10-01"

horses:

1:addr:"北京路159號"

price:"145萬"

rooms:

1:"客廳"

2:"主臥"

3,"次臥1"

這是乙個四層的字典,第二層是人,第三層是房子,第四層是房間,需要引用裡面的資訊時,只需要像這樣就行了:

第二個人的生日:alldic(2)("birthdate")

這種結構是不是似曾相識呢?對的,他就像json。

現在進入正題,這個結構是儲存在記憶體中的,如何打包為字串進行檢視和儲存?而且這個結構在vbe中的本地視窗中是無法展開的,除錯起來很麻煩。

這時我們就需要下面的**來將其打包成結構式的文字,採用了遞迴方法,無論有多少層都能處理哦。

sub nestingdictostring()

dim dict as string

dim parentdic as object

dim treedic as object

dim i,ii

dim str

dim oldkey

textbox1.text = ""

dic = "字典結構" & chr(10)

set parentdic = createobject("scripting.dictionary")

set treedic = createobject("scripting.dictionary")

'先把要打包的字典放到過程變數中:

for each dc in tabdic

parentdic.add dc,tabdic(dc)

next

i = 0

do while i < parentdic.count

key = parentdic.keys

'判斷是否巢狀了字典,如果是,把子字典加到主遍歷中(遞迴)

if typename(parentdic(key(i))) = "dictionary" then

treedic.add treedic.count,key(i) & "i"

for each dc in parentdic(key(i))

parentdic.add key(i) & ";" & dc,parentdic(key(i))(dc)

next

for ii = i + 1 to parentdic.count - parentdic(key(i)).count - 1

'把父字典放到最後,調整順序

oldkey = key(ii)

parentdic.add "-last-",parentdic(oldkey)

parentdic.remove(oldkey)

parentdic.key("-last-") = oldkey

next

else

treedic.add treedic.count,key(i) & ":" & parentdic(key(i))

end if

loop

'下面拼接為字串[chr(9)為tab鍵]:

for ii = 0 to treedic.count - 1

i = ubound(split(treedic(ii),";"))

dict = dict & string(i,chr(9)) & split(treedic(ii),";")(i) & chr(10)

next

textbox1.text = dict

end sub

序列化(模型序列化 序列化巢狀)

from rest framework import serializers from meituan.models import merchant,class merchantserializer serializers.modelserializer class meta model merch...

解決反序列化內部巢狀類未序列化的問題

碰到個問題,http請求介面,返回 response型別資料 fastjson內建反序列化型別方法,直接使用即可 type type new typereference gettype responseres jsonobject.parseobject result,type resttempla...

序列化 檔案的輸入與輸出

本次的討論不涉及資料庫 將序列化物件寫入檔案 1 開啟檔案 fileoutputstream filestream new fileoutputstream mygame.ser 檔案不存在會自動建立 2 鏈結到開啟的檔案 objectoutputstream os new objectoutput...