json字符串的封装(jackson框架)

一、前台封装json对象:


 $("#save").click(function(){
      var objs=$("#tagList .commonTb1 .selectBox");
            var jsonObject = JSON.parse("{\"data\":[]}");
            objs.each(function(index){
          var tagObject={
                  id : "",
                  name : "",
                  status : ""
                 }
          if($(this).hasClass("selectedBox")){
          tagObject.id = $(this).attr("mid");
          tagObject.name = $(this).parent().parent().next().children().val();
          tagObject.status = "1";
          }else{
          tagObject.id = $(this).attr("mid");
          tagObject.name = $(this).parent().parent().next().children().val();
          tagObject.status = "0";
          }
          jsonObject.data.push(tagObject);
         });  

});



二、后台解析json对象:


public boolean saveSelectedTag(String tagjsonObject) throws JSONException{

JSONObject json=new JSONObject(tagjsonObject);
JSONArray jsonArray = json.getJSONArray("data");
List<CmsTag> listTag = new ArrayList<CmsTag>();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject tag = (JSONObject) jsonArray.get(i);
Integer showDirection = Integer.parseInt(tag.get("status").toString());
Integer tagId = Integer.parseInt(tag.get("id").toString());
String directionName = tag.get("name").toString();
if(showDirection==1){
showDirection = DirectionState.SHOW.getDirectionState();
}else{
showDirection = DirectionState.NOTShow.getDirectionState();
}
CmsTag cmsTag = cmsTagDao.findOne(tagId);
cmsTag.setShowDirection(showDirection);
cmsTag.setDirectionName(directionName);
listTag.add(cmsTag);
}
   try{
           cmsTagDao.save(listTag);
           return true;
          }catch(Exception e){
        return false;
         }

 
}


三、jackson解析jsonz数组字符串:


public boolean saveSelectedTag(String tagjsonObject) throws IOException{
ObjectMapper mapper = new ObjectMapper();  
        JsonNode rootNode = mapper.readValue(tagjsonObject, JsonNode.class);   
        //获得结点为rules的集合  
        JsonNode nameNode = rootNode.path("data");        
List<CmsTag> listTag = new ArrayList<CmsTag>();
for (int i = 0; i < nameNode.size(); i++) {
JsonNode tagObject = nameNode.get(i);
Integer showDirection = Integer.parseInt(tagObject.get("status").asText());  //asText(),是去掉对象中的双引号:(eg:" "1" ")
Integer tagId = Integer.parseInt(tagObject.findValue("id").asText());
String directionName = tagObject.findValue("name").asText();
if(showDirection==1){
showDirection = DirectionState.SHOW.getDirectionState();
}else{
showDirection = DirectionState.NOTShow.getDirectionState();
}
CmsTag cmsTag = cmsTagDao.findOne(tagId);
cmsTag.setShowDirection(showDirection);
cmsTag.setDirectionName(directionName);
listTag.add(cmsTag);
}
   try{
           cmsTagDao.save(listTag);
           return true;
          }catch(Exception e){
        return false;
         }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值