• 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());
           }
        }

    }

  • 相关阅读:
    安卓系统浏览器中select下拉按钮无法弹出选择面板奇怪问题解决
    Webkit浏览器点击控件时出现的边框消除
    UML序列图总结
    UML序列图总结
    UML类图几种关系的总结
    UML类图几种关系的总结
    UML用例图总结
    UML用例图总结
    类与类之间的关系
    java核心技术----Object类
  • 原文地址:https://www.cnblogs.com/guoke289/p/9926717.html
Copyright © 2020-2023  润新知