java对象转化为json

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

try {

///< 读取、解析设备列表
String xmlPath = ConfigureProperties.getInstance().getMediaServerPath();

///< 解析xml
Document doc = XmlManager.parse(xmlPath);  //相对路径
   Element element = doc.getDocumentElement();    //获得spots根节点
List<Spot> spots = null; 
   spots = SpotsParser.parser(element);  //解析spots根节点,返回解析列表
   
   List<OnlineDevice> lsonline = displaySpotsOnline(spots);
            
   ///< 发送在线设备到前台
   java.lang.reflect.Type type = 
          new com.google.gson.reflect.TypeToken<List<OnlineDevice>>(){}.getType();
            gson = new Gson();
   String json = gson.toJson(lsonline, type);
   resp.setCharacterEncoding("UTF-8");  

   resp.getWriter().write(json);
   resp.getWriter().flush();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}


public List<OnlineDevice> displaySpotsOnline(List<Spot> ls){
int len = ls.size();
Spot spot =null;
List<OnlineDevice> lsOnDe=new ArrayList<OnlineDevice>();
for(int i =0;i<len;i++){
 spot = ls.get(i);
 Integer spotOnline=spot.getState(); 
 if (1==spotOnline){
 OnlineDevice onDe= new OnlineDevice();
 onDe.setLson(spotOnline);
 onDe.setNameon(spot.getName());
 onDe.setUrl(spot.getUrl());
 //此处的id必须从url中截取出来
 Long spotId = Long.parseLong(onDe.getUrl().substring(0, onDe.getUrl().indexOf("@")));
 onDe.setId(spotId);
 String onLineId = spotId.toString();
 List<SpotsPublish> spotsPublishList = SpotsPublish.getInstance().readPublishState();
 String tempPublishState = "0";
 for(int j=0; j<spotsPublishList.size(); j++ ){
 
 ///注意一下此处的向量能当成二维向量来使,因为此处比较相同的spotid需要传出的是他的发布状态publishState
 String tempId = spotsPublishList.get(j).getStrId(); 
 if(tempId.equalsIgnoreCase(onLineId)){
 tempPublishState = spotsPublishList.get(j).getStrPublishState();
 break;
 }
 }
 onDe.setOnPublish(tempPublishState);
 lsOnDe.add(onDe);
          }
   }
return lsOnDe;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值