java parse json string

本例是最复杂的例子,本文可以从最简单的写起,由浅入深,一步一个例子写。

先从解析 { "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);
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值