zoukankan      html  css  js  c++  java
  • JAVA与JSON的序列化、反序列化

    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();
            }
        }
    }

  • 相关阅读:
    C#和C++除了语法上的差别外,还有什么其他的区别
    各种指针的的概览及造成原因
    批量操作Tomcat Shell脚本
    pi币pinetwork安装注册教程中文详细版【实操有效】
    Oracle分析函数
    Logger.Xml
    使用Redis / Zookeeper作为分布式锁的一些注意点
    Seata Server配置文件
    .gitignore忽略target无效
    MySql隔离级别:RU / RC / RR / S + 脏读 / 不可重复读 / 幻读 / 可重复读
  • 原文地址:https://www.cnblogs.com/javafan/p/3832950.html
Copyright © 2011-2022 走看看