关于json字符串的解析与拼接,第一次接触,留下个笔记了。。。。。。。解析,是改的代码,拼接是纯的,解析就不说了,笔记一下拼接了
关于解析主要分三部分,一个是第一层处理,一个是第二层处理,一个是进行","的处理。其实再往深一层的也可以,我认为,那个代码得修改修改了。
Public Function yiceng(a(), b()) As String
Dim l As Integer
l = UBound(a) - LBound(a) + 1
Dim str As String
str = str & vbCrLf & Space(6) & "{"
For i = 0 To l - 1
If b(i) = "" Then
str = str & Chr(13) & Space(8) & """" & a(i) & """" & ":" & vbCrLf & Space(10) & "["
str = str & vbCrLf & Space(10) & "],"
Else
str = str & Chr(13) & Space(8) & """" & a(i) & """" & ":" & """" & b(i) & """" & ","
End If
Next
str = chuli(str)
str = str & Space(6) & "},"
yiceng = yiceng & str
End Function
Public Function erceng(str As String, a(), b()) As String
Dim l As String
l = UBound(a) - LBound(a) + 1
‘查找[的位置,进行插入操作
Dim i As Integer
i = InStr(1, str, "]")
Dim qstr As String
Dim hstr As String
qstr = Mid(str, 1, i - 1)
hstr = Mid(str, i)
Dim instr As String
istr = yiceng(a(), b())
erceng = qstr & istr & hstr
‘erceng = chuli(erceng)
End Function
‘去掉倒数第一个逗号
Public Function chuli(str As String) As String
Dim y As Integer
y = InStrRev(str, ",")
qstr = Mid(str, 1, y - 1)
hstr = Mid(str, y + 1)
chuli = qstr & hstr
End Function
测试处理
Private Sub Command2_Click()
Dim a1()
Dim b1()
Dim c()
Dim d()
Dim a2()
Dim b2()
Dim a3()
Dim b3()
c() = Array("4a", "5a", "6a")
d() = Array("d", "e", "")
a1() = Array("1a", "2a", "3a")
b1() = Array("a", "b", "c")
a2() = Array("a", "a", "a")
b2() = Array("a1", "b1", "c1")
a3() = Array("aa", "aa", "aa")
b3() = Array("a1", "b1", "c1")
Dim stri As String
stri = yiceng(c(), d())
stri = erceng(stri, a1(), b1())
stri = erceng(stri, a2(), b2())
stri = erceng(stri, a3(), b3())
stri = chuli(stri)
stri = chuli(stri)
MsgBox stri
Text1.Text = stri
End Sub
在有子目录的地方对应位置为空,最后两回去“,”