您的位置:首页 > Web前端 > JavaScript

对json格式的数据进行解析

2016-01-18 15:48 776 查看
public String updateAlbumAndVideoRelation(String param) throws TException {
logger.info("[ProgramRpcManager][updateAlbumAndVideoRelation][step:start][param={}]",param);
String result = "";
try{
JSONArray relationJsonArr=JSONArray.fromObject(param);
if(relationJsonArr==null)//传递空数据
{
result=Constants.getResponseStr(Constants.CODE_FAIL,"传递的参数错误,参数为空");
}else{
for(int i=0;i<relationJsonArr.size();i++)
{
JSONObject relationObj=relationJsonArr.getJSONObject(i);
Long qipuId=relationObj.getLong("qipuId");//不允许为空

Long albumId=null;
Integer videoOrder=null;
boolean isBound=relationObj.getBoolean("isBound");
if(isBound)//当需要进行绑定时候获取专辑ID以及videoOrder
{
String videoOrderStr=relationObj.getString("videoOrder");
videoOrder  = StringUtil.toInteger(videoOrderStr, 1);
albumId=relationObj.getLong("albumId");//
}
Video video=videoService.getByQipuId(qipuId);//获取对应的视频
if(video==null||video.getId()==null){
result=Constants.getResponseStr(Constants.CODE_ERROR_NOT_EXIST_PROGRAM,"不存在该视频");
}else {
//更新视频与专辑的关系
if (videoService.updateAlbumAndVideoRelation(video, albumId, videoOrder)) {
result = Constants.getResponseStr(Constants.CODE_SUC);
} else {
result = Constants.getResponseStr(Constants.CODE_FAIL);
}
}
}
}

}catch(Exception e){
//接受的数据不是JSONArray类型则抛出异常
result = Constants.getResponseStr(Constants.CODE_FAIL,"传递的数据不完整:"+ e.getMessage());
logger.error("[ProgramRpcManager][updateAlbumAndVideoRelation][step:exception][param={}]",param,e);
}
return result;
}
对json格式的数据进行解析,如果出现值为空,例如标红部分,则会出现解析异常,此时线程会继续运行,并不会终止。
{'isBound':false,'qipuId':90009900,'albumId':,'videoOrder':null},
* {'isBound':true,'qipuId':90010100,'albumId':50000052,'videoOrder':1}]"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: