想要将javascript序列化的树数组转换为可读格式

编程入门 行业动态 更新时间:2024-10-26 11:28:51
本文介绍了想要将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序列化的树数组转换为可读格式

本文发布于:2023-11-11 06:33:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   转换为   可读   格式   序列化

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!