• Java json串生成及转bean


     
    package com;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import net.sf.ezmorph.bean.MorphDynaBean;
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import net.sf.json.JSONSerializer;
    public class Fetion {
        
        public static void main(String[] ss) {
            test2();
        }
        
        public static void test(){
    
            
            //构建json串
            Map<String,Object> map3 = new HashMap<String,Object>();
            map3.put("id",1234);map3.put("name","zhange");map3.put("age", 43);
            
            List<Object> ls = new ArrayList<Object>();
            ls.add(map3);
            
            Map<String,Object> map2 = new HashMap<String,Object>();
            map2.put( "count" , "1" );
            map2.put( "router" , ls);
            
            Map<String,Object> map = new HashMap<String,Object>();
            map.put( "listroutersresponse" , map2);
               
            JSONObject jsonObject2 = JSONObject.fromObject(map);
            String response = jsonObject2.toString();
            System.out.println("json串值:"+response);
            
            //解析json串
            JSONObject jsonObject = JSONObject.fromObject(response);
            JSONObject listvolumesresponse = (JSONObject) jsonObject.get("listroutersresponse");
            if (listvolumesresponse != null && listvolumesresponse.has("router")) {
                JSONArray router = (JSONArray) listvolumesresponse.getJSONArray("router");
                List<MorphDynaBean> list = (List<MorphDynaBean>) JSONSerializer.toJava(router);
                System.out.println("获取id值:"+list.get(0).get("id").toString());
            }
            System.out.println("---------------------------");
            //构建MorphDynaBean
            MorphDynaBean mdb;
            JSONObject js = new JSONObject();
            js.accumulate("areaid", 421);
            js.accumulate("areaname", "福建");
            mdb = (MorphDynaBean) JSONSerializer.toJava(js);
            System.out.println("构建MorphDynaBean:" + mdb);
            try {
                System.out.println("获取地市名:" + mdb.get("areaname"));
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("---------------------------");
        
        }
        
        public static void test2(){
            //构建json串
            Map<String,Object> map3 = new HashMap<String,Object>();
            map3.put("id",1234);map3.put("name","zhange");map3.put("age", 43);
            
            
            Map<String,Object> map2 = new HashMap<String,Object>();
            map2.put( "count" , "1" );
            map2.put( "router" , map3);
            
            Map<String,Object> map = new HashMap<String,Object>();
            map.put( "listroutersresponse" , map2);
               
            JSONObject jsonObject2 = JSONObject.fromObject(map);
            String response = jsonObject2.toString();
            System.out.println("json串值:"+response);
            
            //解析json串
            JSONObject jsonObject = JSONObject.fromObject(response);
            JSONObject listvolumesresponse = (JSONObject) jsonObject.get("listroutersresponse");
            if (listvolumesresponse != null && listvolumesresponse.has("router")) {
                JSONObject router = listvolumesresponse.getJSONObject("router");
                System.out.println("获取id值:"+router.get("id").toString());
            }
            System.out.println("---------------------------");
            //构建MorphDynaBean
            MorphDynaBean mdb;
            JSONObject js = new JSONObject();
            js.accumulate("areaid", 421);
            js.accumulate("areaname", "福建");
            mdb = (MorphDynaBean) JSONSerializer.toJava(js);
            System.out.println("构建MorphDynaBean:" + mdb);
            try {
                System.out.println("获取地市名:" + mdb.get("areaname"));
                
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("---------------------------");
        
        }
        
    }

    相关jar包下载

  • 相关阅读:
    Linux基础
    杂谈
    MySQL基础
    Effective Java-第4章
    Effective Java-第三章
    Effective Java-第二章
    mybatis
    mapper.xml文件
    Mybatis
    mybatis-config.xml文件详解
  • 原文地址:https://www.cnblogs.com/langdangyunliu/p/5622610.html
Copyright © 2020-2023  润新知