zoukankan      html  css  js  c++  java
  • json---简单入门

    1.推荐使用第三种方式JAVABEAN的方式(使用前引入org.json)

    package day05;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import Bean.Diaosi;
    public class JsonObjectSample {
        public static void main(String[] args) {
            JSONObject1();
            //createJsonByBean();
            //createJsonByMap();
        }
    
        private static void JSONObject1() {
            // TODO Auto-generated method stub
            JSONObject wangxiaoer=new JSONObject();
            Object nullObject=null;
            try {
                wangxiaoer.put("name", "wangxiaoer");
                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", nullObject);
                wangxiaoer.put("comment", "这是一个注释");
                System.out.println(wangxiaoer.toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        //MAP的方式
        private static void createJsonByMap(){
            Map<String,Object> wangxiaoer=new HashMap<String,Object>();
            Object nullObject=null;
            wangxiaoer.put("name", "wangxiaoer");
            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", nullObject);
            wangxiaoer.put("comment", "这是一个注释");
            System.out.println(new JSONObject(wangxiaoer).toString());
        }
        //JAVABEAN的方式
        
    
        private static void createJsonByBean(){
            Diaosi wangxiaoer=new Diaosi();
            Object nullObject=null;
            wangxiaoer.setName("wangxiaoer");
            wangxiaoer.setAge(25.2);
            wangxiaoer.setBirthday("1990-01-01");
            wangxiaoer.setSchool("蓝翔");
            wangxiaoer.setMajor(new String[]{"理发","挖掘机"});
            wangxiaoer.setHas_girlfriend(false);
            wangxiaoer.setCar(nullObject);
            wangxiaoer.setComment("这是一个注释");
            System.out.println(new JSONObject(wangxiaoer));
        }
    }

    bean

    package Bean;
    
    public class Diaosi {
        private String name;
        private String school;
        private boolean has_girlfriend;
        private double age;
        private Object car;
        private String[] major;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSchool() {
            return school;
        }
        public void setSchool(String school) {
            this.school = school;
        }
        public boolean isHas_girlfriend() {
            return has_girlfriend;
        }
        public void setHas_girlfriend(boolean has_girlfriend) {
            this.has_girlfriend = has_girlfriend;
        }
        public double getAge() {
            return age;
        }
        public void setAge(double age) {
            this.age = age;
        }
        public Object getCar() {
            return car;
        }
        public void setCar(Object car) {
            this.car = car;
        }
        public String[] getMajor() {
            return major;
        }
        public void setMajor(String[] major) {
            this.major = major;
        }
        public String getComment() {
            return comment;
        }
        public void setComment(String comment) {
            this.comment = comment;
        }
        public String getBirthday() {
            return birthday;
        }
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
        private String comment;
        private String birthday;
    
    }

    2.解析json

    wangxiaoer.json

    {
    "car":null,
    "birthday": "1990-01-01",
    "school":"蓝翔",
    "has_girlfrend":false,
    "age":25.2,
    "name":"王小二",
    "comment":"这是一个注释",
    "major":["理发","挖掘机"]
    
    }

    readjsonsample.java

    package day05;
    
    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 ReadJSONSample {
        public static void main(String[] args) throws IOException, JSONException {
            File file = new File(ReadJSONSample.class.getResource("/wangxiaoer.json").getFile());
            String content = FileUtils.readFileToString(file);
            JSONObject jsonObject = new JSONObject(content);
            if (!jsonObject.isNull("name")) {
                System.out.println("姓名 :" + jsonObject.getString("name"));
            }
            System.out.println("年龄 :" + jsonObject.getDouble("age"));
            System.out.println("有没有女朋友 :" + jsonObject.getBoolean("has_girlfrend"));
            JSONArray list = jsonObject.getJSONArray("major");
            for (int i = 0; i < list.length(); i++) {
                String m = (String) list.get(i);
                System.out.println("专业-" + (i + 1) + m);
            }
        }
    
    }

     结果:

    姓名 :王小二

    年龄 :25.2

    有没有女朋友 :false

    专业-1理发

    专业-2挖掘机

    既往不恋,未来不迎,当下不杂
  • 相关阅读:
    [EasyUI美化换肤]更换EasyUi图标
    [干货来袭]MSSQL Server on Linux预览版安装教程(先帮大家踩坑)
    SignalR系列续集[系列8:SignalR的性能监测与服务器的负载测试]
    对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)
    EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)
    采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)
    android ViewFlipper(翻转视图) 使用
    Android 下拉列表Spinner 使用
    Android选项卡TabHost功能和用法
    【android,eclipse解决】eclipse insert "}" to complete ClassBodyR.java
  • 原文地址:https://www.cnblogs.com/SnowingYXY/p/6222914.html
Copyright © 2011-2022 走看看