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("---------------------------");
    
    }
    
}
复制代码

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值