本例是最复杂的例子,本文可以从最简单的写起,由浅入深,一步一个例子写。
先从解析 { "key", "value"} 这种开始
数据源
{
Routes = [
{path: '', component: Dashboard},
{path: 'dashboard', redirectTo: ''},
{path: 'call', component: DialerComponent, canActivate: [ConferencingGuard]},
{
path: 'general',
children: [
{path: '', redirectTo: 'region', pathMatch: 'full'},
{path: 'region', component: RegionLanguagePage},
{path: 'language', component: RegionLanguagePage},
]
},
{
path: 'network',
children: [
{path: '', redirectTo: 'ip-address', pathMatch: 'full'},
{path: 'ip-address', component: IpAddressPage},
{path: 'dns', component: DnsPage},
]
},
]}
------------------------------------------------------------------
/*notes: value [ ] 会json parse fail ,改成value ""; value is true/false, it is checkbox; option value is [1,2,3..] is drop list, other is input
try option is list[]*/
/* input is router.txt;
* output is find the insert line key and value*/
public void ParseRouterJson() {
//json格式的字符串
File file = new File("router.txt");
String str = null;
//read/parse json string to find menu/submenu location
try {
//read router.txt
str = FileUtils.readFileToString(file,"GBK");
} catch (IOException e) {
e.printStackTrace();
}
//! 创建一个Gson对象
Gson gson = new Gson();
//deserilization
Obj obj = gson.fromJson(str, Obj.class);
//System.out.println(obj.getVars().get(0).getConfigVar().getNodeAndVarName());
String menu,submenu;
int childsize=0;
int i,j;
//7 main menu+3 head
for (i=0;i<10;i++)
{
menu = obj.getRoutes().get(i).getPath();
if ( menu.equals(menustr.toLowerCase()))
{
childsize=obj.getRoutes().get(i).getChildren().size();
System.out.println("line120:"+childsize);
break;
}
}
for (j=0;j<childsize;j++)
{
submenu = obj.getRoutes().get(i).getChildren().get(j).getPath();
if (submenu.compareTo(submenustr.toLowerCase())>0)
{
break;
}
}
lastpath=obj.getRoutes()
.get(i)
.getChildren()
.get(j-1)
.getPath();
componentstr=obj.getRoutes()
.get(i)
.getChildren()
.get(j-1)
.getComponent();
System.out.println("line144:lastpath="+lastpath+";componentstr="+componentstr+"i="+i+";j="+j);
}