梦相随1006

版权归 梦相随1006 所有,未经 https://www.cnblogs.com/xin1006 作者许可,严禁转载

导航

json-java处理-jackson

使用jackson处理json数据

maven中的配置,这里没有写版本信息

<dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-core-asl</artifactId>
</dependency>
<dependency>
     <groupId>org.codehaus.jackson</groupId>
     <artifactId>jackson-mapper-asl</artifactId>
</dependency>

工具类

package opstools.vtm.support.utils;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

public class JsonSupportUtils {
    
    public final static ObjectMapper jsonMapper = new ObjectMapper();
    
    public static String serialize(Object object) throws Exception{
        return jsonMapper.writeValueAsString(object);
    }
    
    public static <T> T deserialize(String str, Class<T> valueType) throws Exception{
        return (T)jsonMapper.readValue(str, valueType);
    }
    
    public static <T> T deserialize(String str, TypeReference valueTypeRef) throws Exception{
        return (T)jsonMapper.readValue(str, valueTypeRef);
    }
    
}

测试使用

     -->Service层的某个类

    @SuppressWarnings("unchecked")
    @Override
    public String getDicByCodeAndFactoryValue(String code,String factoryValue) throws Exception{
        
        //改成JSON格式的
        List<Object[]> result=  (List<Object[]>) dicdao.getDicByCodeAndFactoryValue(code,factoryValue);
        List<ResourceValue> rvs=new ArrayList<ResourceValue>();
        if(result!=null && result.size()>0){
            for(Object[] obj:result){
                rvs.add(new ResourceValue(obj[0],obj[1]));
            }
return JsonSupportUtils.serialize(rvs); } return null; }

        -->Action层的某个类

 

    /**
     * 从字典中获取设备型号信息(依据设备厂商信息)
     * @return
     * @throws Exception 
     */
    public  void getDeviceTypeJSON() throws Exception{
        String factory=request.getParameter("factory");
        if(factory!=null){
            //根据厂商信息加载设备型号
            String result=dicService.getDicByCodeAndFactoryValue(DIC_DEVICE_MODEL, factory);
//设置json格式 response.setContentType(
"text/json;charset=UTF-8"); response.getWriter().write(result); } }

 

      -->前台界面层的处理,这里是用jquery处理的js

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
       ......
   /**
   *下拉框的级联操作
   */
<script type="text/javascript" > $(document).ready(function(){ $("#deviceFactory").on("change",function(event){ $.post("deviceInfo!getDeviceTypeJSON.jspa", {factory:$(this).val()}, function(data){ if(data!=null){ //1,清空设备型号内部元素 $("#deviceType").empty(); //2,增加提示信息 $("#deviceType").append("<option value=''>请选择</option>"); for(var i=0;i<data.length;i++){ //添加新数据 var option="<option value='"+data[i].realValue+"'>"+data[i].displayValue+"</option>"; $("#deviceType").append(option); } } },"json"); }); }) </script> </head> <body ...... <s:form id="MYFORM" action="%{pageAction}" method="post" theme="simple"> <div id="content"> <table> ...... <tr> <th width="17%">设备厂商:</th> <td width="35%"><s:select name="deviceInfo.factory" id="deviceFactory" list="deviceFactoryList" listKey="realValue" listValue="displayValue" headerKey="" headerValue="请选择"/> </td> <th width="13%">设备型号:</th> <td width="35%"><s:select name="deviceInfo.deviceNo" id="deviceType" list="deviceTypeList" listKey="realValue" listValue="displayValue" headerKey="" headerValue="请选择"/> </td> </tr> </table> </div> </s:form> </body> </html>

 

 

posted on 2014-04-03 09:57  梦相随1006  阅读(436)  评论(0编辑  收藏  举报