我是python的新手,我必须在从文本文件中获取输入后在python中构建一个树,
我在文本文件中包含以下数据.我必须使用Json在python中使用以下数据构建一个树
{
"component": "A",
"status": 0,
"children": [
{
"component": "AA",
"status": 0,
"children": [
{
"component": "AAA",
"status": 0,
"children": []
},
{
"component": "AAB",
"status": 0,
"children": []
}
]
},
{
"component": "AB",
"status": 0,
"children": [
{
"component": "ABA",
"status": 0,
"children": []
},
{
"component": "ABB",
"status": 0,
"children": []
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我写了下面的代码,但它有语法错误,我无法纠正,如果任何人都可以找到它们
class node:
#Construction of Node with component,status and children
def _init_(self,component=None,status=None,children=None):
self.component = component
self.status = status
if children …Run Code Online (Sandbox Code Playgroud) 我需要将元素放在Map中,然后选择TreeMap实现,键将在地图中按升序排列,但其中一个"未分配"的键应该始终是第一个.这可能吗 ?我当前的代码只将元素放在排序顺序中?
public class TreeMapTest {
public static void main(String args[]){
//the treemap sorts by key
Map<String, String> hm = new TreeMap<String, String>(new StringComparator());
//add key-value pair to TreeMap
hm.put("carrot","12");
hm.put("apple", "34");
hm.put("domboi","912");
hm.put("unassigned","?");
hm.put("banana", "45");
hm.put("zucchini","87");
System.out.println("TreeMap Entries:");
System.out.println(hm);
}
}
class StringComparator implements Comparator<String>{
@Override
public int compare(String str1, String str2) {
return str1.compareTo(str2);
}
}
Run Code Online (Sandbox Code Playgroud)
目前的输出是
{apple=34, banana=45, carrot=12, domboi=912, unassigned=?, zucchini=87}
Run Code Online (Sandbox Code Playgroud)
我希望输出为
{unassigned=?,apple=34, banana=45, carrot=12, domboi=912,zucchini=87}
Run Code Online (Sandbox Code Playgroud) 我有一个像数据树一样的json对象.我想以交互方式在UI上显示它.单击组件时,应显示其子项,依此类推.我写了以下代码
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.9.1.js"></script>
</head>
<body>
<div>
<ul id="result"></ul>
</div>
<script>
var json_data = {
"component": "A",
"status": 0,
"children": [
{
"component": "AA",
"status": 0,
"children": [
{
"component": "AAA",
"status": 0,
"children": []
},
{
"component": "AAB",
"status": 2,
"children": []
}
]
},
{
"component": "AB",
"status": 0,
"children": [
{
"component": "ABA",
"status": 0,
"children": []
},
{
"component": "ABB",
"status": 1,
"children": []
}
]
}
]
};
$(document).ready($(function(){
var $result = …Run Code Online (Sandbox Code Playgroud)