想要将javascript序列化的树数组转换为可读格式 [英] want to convert javascript serialized tree array to readable format
本文介绍了想要将javascript序列化的树数组转换为可读格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下格式的数据:
I have a data in this format:
=[{"Name":"A","children":[{"Name":"C","children":[{"Name":"CC",children":[{"Name":"TC","count":1,"children":
我希望它以可读/xml格式使其易于理解,例如A:C:CC:TC
然后是A:B:BB
等
例如:
I want it in readable/xml format to make it easily understand like A:C:CC:TC
and then A:B:BB
etc
for example:
category - clothes
sub category - ladies, gents
sub category of gents - shirt, pant
sub category of ladies - shirt, pant
现在我想要这种格式
now i want it in this format
clothes - gents - shirt
clothes - gents - pant
clothes - ladies - ...
等
thanx
已添加代码标签-LOSMAC [/EDIT]
etc
thanx
Code tags added - LOSMAC[/EDIT]
推荐答案
我发现了一段时间,并在VBA中为您写了一些东西.记住,我的评论仍然是最新的.
I have found a moment of time and i wrote something for you in VBA. Remember, my comment is still current.
Option Explicit
Sub Test()
Dim sMsg As String
Dim sLines() As String, i As Integer
Dim sCats() As String, j As Integer
'let your input-string looks like:
sMsg = "=[{""Name"":""clothes"",""children"":[{""Name"":""ladies"",""children"":[{""Name"":""shirts"",""children"":[{""Name"":""Orange"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""clothes"",""children"":[{""Name"":""ladies"",""children"":[{""Name"":""pants"",""children"":[{""Name"":""Violet"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""clothes"",""children"":[{""Name"":""gents"",""children"":[{""Name"":""shirts"",""children"":[{""Name"":""Orange"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""clothes"",""children"":[{""Name"":""gents"",""children"":[{""Name"":""pants"",""children"":[{""Name"":""Violet"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""watches"",""children"":[{""Name"":""ladies"",""children"":[{""Name"":""HUGO BOSS"",""children"":[{""Name"":""One"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""watches"",""children"":[{""Name"":""ladies"",""children"":[{""Name"":""HUGO BOSS"",""children"":[{""Name"":""Two"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""watches"",""children"":[{""Name"":""gents"",""children"":[{""Name"":""HUGO BOSS"",""children"":[{""Name"":""Waterfall"",""count"":1}]}]}]}]" & vbCrLf & _
"=[{""Name"":""watches"",""children"":[{""Name"":""gents"",""children"":[{""Name"":""HUGO BOSS"",""children"":[{""Name"":""Niagara"",""count"":1}]}]}]}]"
'we need to get every single line
sLines() = Split(sMsg, vbCrLf)
'now we can clear variable, to use it later
sMsg = ""
For i = LBound(sLines()) To UBound(sLines())
'get all categories and sub-categories
sCats() = ConvertToArray(sLines(i))
For j = LBound(sCats()) To UBound(sCats())
sMsg = sMsg & sCats(j) & "->"
Next
sMsg = Left(sMsg, Len(sMsg) - 2)
sMsg = sMsg & vbCr
Next
'view result
MsgBox sMsg
End Sub
'returns all categories and sub-categories in array
Public Function ConvertToArray(sInputString As String) As String()
Dim sPattern As String, sRetVal() As String
On Error GoTo Err_ConvertToArray
sPattern = sInputString
'remove '=[{"Name":"' to set category name as a first sign
sPattern = Replace(sPattern, "=[{""Name"":""", "")
'remove '","children":[{"Name":"' to get sub-category name and replace with ';'
sPattern = Replace(sPattern, """,""children"":[{""Name"":""", ";")
'get count of item
sPattern = Replace(sPattern, """,""count"":", ";count=")
'remove closed brackets
sPattern = Replace(sPattern, "}]", "")
'return an array of categories and subcategories
sRetVal() = Split(sPattern, ";")
Exit_ConvertToArray:
On Error Resume Next
ConvertToArray = sRetVal()
Exit Function
Err_ConvertToArray:
MsgBox Err.Description, vbExclamation, Err.Number
Resume Exit_ConvertToArray
End Function
结果:
Results:
clothes->ladies->shirts->Orange->count=1<br />
clothes->ladies->pants->Violet->count=1<br />
clothes->gents->shirts->Orange->count=1<br />
clothes->gents->pants->Violet->count=1<br />
watches->ladies->HUGO BOSS->One->count=1<br />
watches->ladies->HUGO BOSS->Two->count=1<br />
watches->gents->HUGO BOSS->Waterfall->count=1<br />
watches->gents->HUGO BOSS->Niagara->count=1<br />
这篇关于想要将javascript序列化的树数组转换为可读格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文