使用RestTemplate.getForObject 解析xml的json页面

三种情境:

1 需要解析的URL内容如下:

<!-- 样例 1-->

<tests>
  <core>25</core>
  <mb>19732</mb>
  ....
</tests>

解析方式:

#新建一个类AppBoot
public class AppBoot{

#此处一定要注意 'tests' 必须与解析页面的 tests保持一致,一旦不一致,取取来的值百分百为null
      private AppTest tests;
     
     #此处省略的是 gettter  and  setter  ,实际应用中要加上
}


#===============
#新建AppTest类
public class AppTest{
   String  core;
   String  mb;

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}

2 需要解析的URL内容如下:

<!-- 样例 2-->

<tests>
    <test>
         <core>25</core>
         <mb>19732</mb>
         ....
    </test>
    <test>
         <app>25</app>
         <id>we_123</id>
         ....
    </test>
    <test>
         ....
    </test>
</tests>

解析方式:

# 1 新建一个根类AppBootRoot
public class AppBottRoot{
#此处一定要注意 'tests' 必须与解析页面的 tests保持一致,一旦不一致,取来的值百分百为null
     private AppBoot tests;

     #此处省略的是 gettter  and  setter  ,实际应用中要加上

}

# 2 新建一个子类AppBoot
public class AppBoot{

#此处一定要注意 'test' 必须与解析页面的 test保持一致,一旦不一致,取来的值百分百为null
      private List<AppBootXML> test;
     
     #此处省略的是 gettter  and  setter  ,实际应用中要加上
}


#===============
# 3 新建AppBootXML子类 选取页面中所需字段
public class AppBootXML{
   String  core;
   String  mb;

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}

# 4 调用方法  填入URL,以及构建的根类AppBootXML.class
AppBootXML rst = RestTemplate.getForObject(//填入需要解析的url,AppBootXML.class)
<AppBootXML> testList = rst.getTests().getTest();
# 可选 按照给定字段排序  
Collections.soft(testList,Comparator.comparing(AppTestXML::getCore));
HashMap<String,AppTestXML> testMap = new HashMap<>();
Iterator<AppTestXML> appTestXML = testList.iterator();
while(appTestXML.hasNext){
       AppTestXML test = appTestXML.next();
       String core = test.getCore;
       String mb = test.getMb();
  
       testMap.put("core",100);
       testMap.put("mb",1442424424);  //可以构建一个方法
      
}

3 需要解析的URL内容如下:

<!-- json格式样例 3-->

{
 "scheduler":{
 "schedulerInfo":{
       "type":"adffff"
       "queues":{
            "queue":[
                 {
                "type":"wewere"
                "users":{
                  "user":[
                           {
                            "username":"test"
                            "vCore":100
                            }
                         ]
                           }
                  }
                    ]
  
                }
                 }
              }
}

解析方式:

# 1 新建一个根类SchedulerInfo
public class SchedulerInfo{
#此处一定要注意 'scheduler' 必须与解析页面的 scheduler保持一致
     private AppScheduler scheduler;

     #此处省略的是 gettter  and  setter  ,实际应用中要加上

}

# 2 新建一个子类AppScheduler 
public class AppScheduler{

#此处一定要注意 'schedulerInfo' 必须与解析页面的 schedulerInfo保持一致
      private AppSchedulerInfo schedulerInfo;
     
     #此处省略的是 gettter  and  setter  ,实际应用中要加上
}

# 3 新建AppSchedulerInfo 子类 选取页面中所需字段
public class AppSchedulerInfo {
   String  core;
   String  mb;
   AppQueues queues;  //注意此处

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}


# 4 新建AppQueues 子类 选取页面中所需字段
public class AppQueues {
 private AppQueue[] queue;  //注意此处  queue 必须与解析页面的queue保持一致,下同。

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}


# 5 新建AppQueue子类 选取页面中所需字段
public class AppQueue{
 AppUsers users;  //注意此处

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}

# 6 新建AppUsers 子类 选取页面中所需字段
public class AppUsers {
private AppUser[] user;  //注意此处

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}

# 7 新建AppUser 子类 选取页面中所需字段
public class AppUser {
Object username;  //注意此处 只能是Object类型,否则会报错

#此处省略的是 gettter  and  setter  ,实际应用中要加上

}

=============================
# 8 调用方法

参考解析2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卢子墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值