当前位置:编程学习 > JS >>

项目用到的Json字串到Json对象转换

如下代码:
[java]
StringBuilder sb = new StringBuilder("["); 
sb.append("{"); 
sb.append("cremindTitle:\"小名\","); 
sb.append("cremindDate:\"2012-06-05 19:20:22\","); 
sb.append("cremindRemark:\"阿斯蒂芬\""); 
sb.append("}"); 
sb.append("]"); 
List<Cremind> clist = JSONArray.toList(JSONArray.fromObject(sb.toString()), 
        Cremind.class); 

在执行中会报错,原因是由于日期类型字串到日期类型Timestamp的转换,解决思路如下:
编写一个Morpher类继承自AbstractObjectMorpher
[java] 
class TimeStampMorpher extends AbstractObjectMorpher{ 
     
    private Logger LOG = Logger.getLogger(TimeStampMorpher.class); 
     
    private String format ="yyyy-MM-dd hh:mm:ss"; 
    public void setFormat(String format) { 
        this.format = format; 
    } 
     
    public TimeStampMorpher() { 
        // TODO Auto-generated constructor stub 
    } 
     
    public TimeStampMorpher(String format) { 
        this.format = format; 
    } 
 
    @Override 
    public Object morph(Object dateStr) { 
        // TODO Auto-generated method stub 
         
        try {        
            java.util.Date date = new SimpleDateFormat(format).parse(dateStr.toString()); 
            return new Timestamp(date.getTime()); 
        } catch (ParseException e) { 
            // TODO Auto-generated catch block 
            if (LOG.isInfoEnabled()) 
                LOG.info("日期格式可能无效,将采用系统当前日期"); 
            e.printStackTrace(); 
        } 
        return new  java.sql.Timestamp(System.currentTimeMillis());//返回默认日期      
    } 
 
    @Override 
    public Class morphsTo() { 
        // TODO Auto-generated method stub 
        return Timestamp.class; 
    } 

最终处理版本:
[java] 
package com.defshare.foundation.global; 
 
import java.sql.Timestamp; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.List; 
 
import org.apache.log4j.Logger; 
 
import net.sf.ezmorph.object.AbstractObjectMorpher; 
import net.sf.json.JSONArray; 
import net.sf.json.util.JSONUtils; 
 
class TimeStampMorpher extends AbstractObjectMorpher{ 
     
    private Logger LOG = Logger.getLogger(TimeStampMorpher.class); 
     
    private String format ="yyyy-MM-dd hh:mm:ss"; 
    public void setFormat(String format) { 
        this.format = format; 
    } 
     
    public TimeStampMorpher() { 
        // TODO Auto-generated constructor stub 
    } 
     
    public TimeStampMorpher(String format) { 
        this.format = format; 
    } 
 
    @Override 
    public Object morph(Object dateStr) { 
        // TODO Auto-generated method stub 
         
        try {        
            java.util.Date date = new SimpleDateFormat(format).parse(dateStr.toString()); 
            return new Timestamp(date.getTime()); 
        } catch (ParseException e) { 
            // TODO Auto-generated catch block 
            if (LOG.isInfoEnabled()) 
                LOG.info("日期格式可能无效,将采用系统当前日期"); 
            e.printStackTrace(); 
        } 
        return new  java.sql.Timestamp(System.currentTimeMillis());//返回默认日期      
    } 
 
    @Override 
    public Class morphsTo() { 
        // TODO Auto-generated method stub 
        return Timestamp.class; 
    } 

 
public final class JsonUtil { 
     
    /**
     * json数组字串到json数组对象<br/>
     * <pre>
     *  StringBuilder sb = new StringBuilder("[");
     *  sb.append("{");
     *  sb.append("cremindTitle:\"xiaoqiang\",");
     *  sb.append("cremindDate:\"2012-06-0519:20:22\",");
     *  sb.append("cremindRemark:\"xiaoqiangremark\"");
     *  sb.append("}");
     *  sb.append("]");
     * </pre>
     * @param jsonArray
     * @param classzz
     */ 
    public static final <T>  List<T>  jsonArrayToList(String jsonArray,Class classzz){ 
        JSONUtils.getMorpherRegistry().registerMorpher(

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,