zoukankan      html  css  js  c++  java
  • java to Json or Json to JavaBean

    今天练习,放这里,以后再补充

    这里使用的jar包是 net.sf.json.JSONObject

    package yh.test.t1118;
    
    
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import yh.com.entity.User;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by Administrator on 2015/11/18.
     */
    public class Test {
    
        public static void main(String[] args) {
    
    //        javaToJson();
    //        JsonToJava();
            test1();
        }
    
    
        /**
         * java to json
         */
        public static void javaToJson(){
            String json = "{"Json":"json"}";
            JSONObject jsonObj = JSONObject.fromObject(json);
            Map<String, String> user = new HashMap<String, String>();
            user.put("name", "jack");
            user.put("age", "21");
            user.put("sex", "male");
            user.put("job", "students");
            jsonObj.put("users",user);
            System.out.println("------开始输出json------");
            System.out.println(jsonObj);
        }
    
        /**
         * json to java
         */
        public static void JsonToJava(){
            String json = "{x:'1',y:'2',userId:'112',element:[{id:'123',name:'haha'},{id:'456',name:'hehe'}]}";
    
    //        JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject = JSONObject.fromObject(json);
    
            jsonObject.element("name","limei");
            jsonObject.element("sex","female");
            jsonObject.accumulate("jj", "ss");
    
            Map<String,String> map = new HashMap<String, String>();
            map.put("a","A");
            map.put("b","B");
            map.put("c","C");
            jsonObject.element("user",map );
    //        System.out.println(jsonObject.toString());
    
            Object o = jsonObject.get("user");
    
    //        System.out.println("==================>"+o);
    //        System.out.println("name:"+jsonObject.get("name"));
            JSONArray jsonArray = jsonObject.getJSONArray("element");
            for(int i = 0;i<jsonArray.size();i++){
                System.out.println(jsonArray.get(i));
            }
    
    //        Object object = JSONObject.toBean(jsonObject);
    //        System.out.println(object.toString());
        }
    
        /*******************将json反序列化为javabean*****************/
        public static void test1(){
    
            String jsonStr = "{x:1,"userId":"112",element:[{id:'123',name:'haha'},{id:'456',name:'hehe'}]}";
            Map<String,Class<?>> m  = new HashMap<String,Class<?>>();
            m.put("x", Integer.class);
            m.put("userId", String.class);
            m.put("element",Element.class);
    
            Jsontobean myBean  = (Jsontobean)JSONObject.toBean( JSONObject.fromObject(jsonStr), Jsontobean.class, m);
            System.out.println("x: " + myBean.getX());
            System.out.println("userId: " + myBean.getUserId());
    
            for(Element e : myBean.getElement()){
                System.out.println(e.getId() +"," + e.getName());
            }
        }
    
    }
  • 相关阅读:
    博客园第一篇随笔css3动画(奔跑的小杨)
    Python输出菱形
    Android开发经验总结
    Android中Activity共享变量的另一方法:Application context
    system()与execv()函数使用详解
    Sublime Text2 编译和运行C/C++程序(windows)
    Android View.post(Runnable )
    Android图像处理之Bitmap类
    android中dip、dp、px、sp和屏幕密度
    System.setProperty and System.getProperty
  • 原文地址:https://www.cnblogs.com/yangh965/p/4976231.html
Copyright © 2011-2022 走看看