使用python将分隔的字符串转换为分层JSON

时间:2018-02-13 04:41:32

标签: python

我有字符串数据:“1233/333 / test1.js,1233/333 / test2.js,2222/44 / test2.js,ss.js”`

并希望输出看起来像:

import { of } from 'rxjs/Observable/of';

对此提出任何建议表示赞赏。

1 个答案:

答案 0 :(得分:0)

output = []
casesList = "1233/333/test1.js,1233/333/test2.js,2222/44/test2.js,ss.js".split(",")
for item in casesList:
    chain = item.split('/')
    currentNode = output
    for index,node in enumerate(chain):
        wantedNode = node
        lastNode = currentNode
        i = 0
        for x in range(i,len(currentNode)):
            i = x + 1
            if currentNode[x]["title"] == wantedNode:
                currentNode = currentNode[x]["children"]
                break
        if lastNode == currentNode:
            if '.' in wantedNode:
                currentNode.insert(i,{"title":wantedNode,"children":[]}) 
            else:
                currentNode.insert(i,{"title":wantedNode,"folder":True,"expanded":True,"children":[]}) 
            newNode = currentNode[i]
            currentNode = newNode["children"]

print(output)