|
我想 利用VBA把当前目录下的所有JSON文件,按着指定的格式解析到xls 文件中,在网友的帮助下,解决了我的需求,
但实际遇到的部分JSON文件中可能有多条需要的内容,现有代码只能输出第一条,而实际需要是输出所有的,有的是一条,有的有多条,
我是小白,不懂VBA,希望得到帮助,谢谢!
现有代码如下:
Private Sub test()
Dim ado As Object, reg As Object
Dim ph$, s$, p$, ary(), i%, j%
Set reg = CreateObject("vbscript.regexp")
Set ado = CreateObject("adodb.stream")
ph = ThisWorkbook.Path & "\*.json" '文件目录
p = Dir(ph)
With ado
.Charset = "Utf-8"
.Type = 2
.Mode = 3
End With
i = 0
ReDim Preserve ary(i)
ary(i) = "seriesId|seriesName|programType|playURL"
While p <> ""
ado.Open
ado.LoadFromFile ThisWorkbook.Path & "\" & p
s = ado.ReadText
i = i + 1
ReDim Preserve ary(i)
For j = 0 To 3
reg.Pattern = Split(ary(0), "|")(j) & ".*""(.*)"","
ary(i) = ary(i) & reg.Execute(s)(0).SubMatches(0) & "|"
Next
ado.Close
p = Dir
Wend
[a1].Resize(UBound(ary) + 1) = Application.Transpose(ary)
Range("a:a").TextToColumns other:=True, otherchar:="|"
Set reg = Nothing
Set ado = Nothing
End Sub
要实现的数据格式如下
示例文件
json.rar
(20.27 KB, 下载次数: 8)
|
|