码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA与JSON的序列化、反序列化

时间:2014-07-11 23:36:32      阅读:391      评论:0      收藏:0      [点我收藏+]

标签:java   os   for   new   amp   re   

package com.linkage.app.memcache;

import java.util.HashMap;
import java.util.Map.Entry;

import net.sf.ezmorph.Morpher;
import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.bean.BeanMorpher;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.apache.taglibs.standard.lang.jpath.example.Person;

import com.linkage.app.vo.admin.ModuleRoleBean;

public class JsonTester {
    public static void main(String[] args) {
        HashMap<String, ModuleRoleBean> map = new HashMap<String, ModuleRoleBean>();

        ModuleRoleBean bean = new ModuleRoleBean();
        bean.setComments("fdsafsafsa");
        bean.setModuleId(323);
        bean.setModuleName("zhou");
        // bean.setStateDate(new Date(System.currentTimeMillis()));
        map.put("1", bean);
        map.put("32", bean);

        JSONObject jsonObject = JSONObject.fromObject(map);
        String json = jsonObject.toString();
        System.out.println(json);
        System.out.println("################");

        JSONObject fromObject = JSONObject.fromObject(json);
        HashMap<String, ModuleRoleBean> retVal = (HashMap<String, ModuleRoleBean>) JSONObject.toBean(fromObject,
                map.getClass());

        MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
        Morpher dynaMorpher = new BeanMorpher(ModuleRoleBean.class, morpherRegistry);
        morpherRegistry.registerMorpher(dynaMorpher);

        for (Entry<String, ModuleRoleBean> entry : retVal.entrySet()) {
            System.out.println("@@" + entry.getKey());
            // System.out.println(entry.getValue().getClass());
            System.out.println("@@" + entry.getValue());
            System.out.println("@@" + morpherRegistry.morph(ModuleRoleBean.class, entry.getValue()));
            System.out.println();
        }
    }
}

JAVA与JSON的序列化、反序列化,布布扣,bubuko.com

JAVA与JSON的序列化、反序列化

标签:java   os   for   new   amp   re   

原文地址:http://www.cnblogs.com/javafan/p/3832950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!