zoukankan      html  css  js  c++  java
  • java 对象 json 集合 数组 互转

    1、先定义一个类对象

    package com.basics;
    
    import com.alibaba.fastjson.JSONObject;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author admin
     */
    public class students {
        private Integer id;
        private String uname;
        private String hobby;
        private String dream;
        private List<Map<String,String>> info;
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getUname() {
            return uname;
        }
    
        public void setUname(String uname) {
            this.uname = uname;
        }
    
        public String getHobby() {
            return hobby;
        }
    
        public void setHobby(String hobby) {
            this.hobby = hobby;
        }
    
        public String getDream() {
            return dream;
        }
    
        public void setDream(String dream) {
            this.dream = dream;
        }
    
        public List<Map<String, String>> getInfo() {
            return info;
        }
    
        public void setInfo(List<Map<String, String>> info) {
            this.info = info;
        }
        
        @Override
        public String toString(){
            return "{"id":""+ id+ "","uname":""+uname+"","hobby":""+hobby+"","dream":""+dream+"","info":"+ JSONObject.toJSONString(info) +"}";
        }
    }
    

      

    2、实例对象添加数据和转json

    package com.basics;
    
    import net.sf.json.JSONObject;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class objectMap {
    
        public static void main(String[] args){
            students std = new students();
            std.setId(123456);
            std.setUname("ouba");
            std.setHobby("rap");
            std.setDream("miss");
            List<Map<String,String>> info = new ArrayList<Map<String,String>>();
            Map<String,String> m = new HashMap<String,String>();
            m.put("名字1","欧巴");m.put("name2","哥哥");
            info.add(m);
    
            std.setInfo(info);
    
            System.out.println(std.toString());
            //对象转json
    //        JSONObject json = JSONObject.parseObject(std.toString());
    //        //List<String> line = (List<String>) json.get("info");
    //        JSONArray line = (JSONArray) json.get("info");
    //        System.out.println(line.get(1));
            JSONObject js = JSONObject.fromObject(std);
            String st = js.toString();
            System.out.println(st);
        }
    }
    

      

    注意,使用JSONObject 需要导入json-lib包,下载地址:链接: https://pan.baidu.com/s/1_TJqAvrth8yO2p-a-N33JA 提取码: q182 复制这段内容后打开百度网盘手机App,操作更方便哦

  • 相关阅读:
    IOC Unity的配置问题
    编译时常量与运行时常量
    Revit二次开发,将插件按钮(Ribbon)变灰或者隐藏
    C#类库读取App.config配置文件
    winform固定窗体大小
    Revit二次开发,获取模型版本信息
    JavaScript:文件保存自動下載函數:Save和SaveAs
    JavaScript:年月日時分秒設置
    JavaScript:字符串の空格刪減和字符刪減功能
    JavaScript:獲取數據の類型
  • 原文地址:https://www.cnblogs.com/wuzaipei/p/10826701.html
Copyright © 2011-2022 走看看