zoukankan      html  css  js  c++  java
  • JSON

    JSON 的存些,需要使用org.json包和common-io包:下面内容来自慕课

    // 存JSON 的方法:共有三种

    import java.util.HashMap;
    import java.util.Map;
    import org.json.JSONException;

    public class JsonObjectSample {

        public static void main(String[] args) {
            // TODO Auto-generated method stub
        // JSONObject();第一种
       creatJSonbymap();//第二中
        }

    // 以下为JSON数据
    //    {
    //         "name": "王小二",
    //         "age":25.2,
    //         "birthday":"1990-01-01",
    //         "school":"蓝翔",
    //         "major":["理发","挖掘机"],
    //         "has_girlfriend":fase,
    //         "car":null,
    //         "house":null,
    //         "comment":"这是一个注释"
    //        
    //        }
        private static void JSONObject() {
            // TODO Auto-generated method stub
            org.json.JSONObject wangxiaoer=new org.json.JSONObject();
            try {
                Object string=null;
            wangxiaoer.put("name", "王小二");
            wangxiaoer.put("age",25.2 );
            wangxiaoer.put("birthday","1990-01-01");
            wangxiaoer.put("school", "蓝翔");
            wangxiaoer.put("major", new String[] {"理发","挖掘机"});
            wangxiaoer.put("has_girlfriend", false);
            wangxiaoer.put("car", string);
            wangxiaoer.put("house", string);
            wangxiaoer.put("comment", "这是一个注释");
            System.out.println(wangxiaoer.toString());
            }catch(JSONException e) {
                e.printStackTrace();
            }
            
            
        }

    //第二中使用Hashmap
        private static void creatJSonbymap() {
            Map<String,Object> wangxiaoer=new HashMap<String,Object>();
            wangxiaoer.put("name", "王小二");
            wangxiaoer.put("age",25.2 );
            wangxiaoer.put("birthday","1990-01-01");
            wangxiaoer.put("school", "蓝翔");
            wangxiaoer.put("major", new String[] {"理发","挖掘机"});
            wangxiaoer.put("has_girlfriend", false);
            wangxiaoer.put("car", null);
            wangxiaoer.put("house", null);
            wangxiaoer.put("comment", "这是一个注释");
            System.out.println(new org.json.JSONObject(wangxiaoer));
        }
        //第三种 使用JavaBean方式构建JSON,通过set和get方法代码胜率;
    }

    ///***********************************************读取JSON文件********************************************

    import java.io.File;
    import java.io.IOException;

    import org.apache.commons.io.FileUtils;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    public class readJSON {

        public static void main(String[] args) throws IOException, JSONException {
            // TODO Auto-generated method stub
           File file=new File(readJSON.class.getResource("/wangxiaoer.json").getFile());
           @SuppressWarnings("deprecation")
        String content=FileUtils.readFileToString(file);
           JSONObject jsonObject=new JSONObject(content);
           System.out.println(jsonObject.get("name"));
          // jsonObject.getDouble();根据类型选择合适的方法得到相应的值,自由选择;
           JSONArray majorArray=jsonObject.getJSONArray("major");
           for(int i=0;i<majorArray.length();i++) {
               System.out.println("专业: "+majorArray.get(i).toString());
           }
        }

    }

  • 相关阅读:
    SQL Server 灾难恢复31天之第3天:在还原数据库时确定需要哪些备份文件
    发布订阅延迟故障排查案例:分发读进程延迟
    [分享]系统crash后SQL Server 在recovery时的rollback机制
    统计信息对执行计划的影响
    [荐][转]SQL SERVER SQLOS的任务调度
    SQL Server 灾难恢复31天之第1天:DBCC CHECK命令会自动使用已经存在的数据库快照吗?
    日志文件如何影响我数据库的启动
    SQL Server 2012 正式发布
    SQL Server 灾难恢复31天之第6天:管理区分配页损坏处理
    [转]Working Set和Private Bytes
  • 原文地址:https://www.cnblogs.com/guoke289/p/9926717.html
Copyright © 2011-2022 走看看