JSON简单快速入门

作者: Mr_欢先生 | 来源:发表于2017-05-08 16:51 被阅读184次

    一.什么是JSON

    JSON是一种与开发语言无关的,轻量级的数据格式。全称 JavaScript Object Notation

    1.标准的JSON数据表示:
    • 数据结构:

    object
    Array

    • 基本类型:

    String
    number
    true
    false
    null

    2.Objext

    使用花括号{ }包含的键值对结构,Key必须为String类型,value为任何基本类型或者数据结构。

    3.array 和 value
    4.String 和 number

    二.如何通过JavaBean构建Json对象

    百度云链接:http://pan.baidu.com/s/1eRK5IAe 密码:badq
    提供了除JSONObject的jar之外依赖的其他6个jar包,一共7个jar文件

    javaBeanJson.java
    package com.huan.json;
    
    /**
    * Created by pc on 2017/5/8.
    */
    public class javaBeanJson {
       private String name;
       private String school;
       private boolean hasGirlfriend;
       private double age ;
       private Object car;
       private Object house;
       private String [] major;
       private String comment;
       private String birthday;
    
       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 isHasGirlfriend() {
           return hasGirlfriend;
       }
    
       public void setHasGirlfriend(boolean hasGirlfriend) {
           this.hasGirlfriend = hasGirlfriend;
       }
    
       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 Object getHouse() {
           return house;
       }
    
       public void setHouse(Object house) {
           this.house = house;
       }
    
       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;
       }
    }
    
    
    jsonObject.java
    package com.huan.json;
    
    import net.sf.json.JSONObject;
    import org.junit.Test;
    
    /**
     * Created by pc on 2017/5/8.
     */
    public class jsonObject {
    
        private static void createJsonBean(){
            javaBeanJson javabeanjson = new javaBeanJson();
            javabeanjson.setName("张飞");
            javabeanjson.setAge(25.0);
            javabeanjson.setBirthday("1999-09-29");
            javabeanjson.setSchool("西安交通大学");
            javabeanjson.setMajor(new String[]{"软件工程","通信工程"});
            javabeanjson.setHasGirlfriend(false);
            javabeanjson.setCar(null);
            javabeanjson.setHouse(null);
            JSONObject json =  JSONObject.fromBean(javabeanjson);
    
            System.out.println(json.toString());
        }
    
        public static void main(String[] args) {
            createJsonBean();
        }
    }
    
    
    打印输出:
    json解析:

    三.如何读取解析Json对象

    导包commons-io-2.5-bin.zip下载:

    链接:http://pan.baidu.com/s/1eRDKf8Q 密码:a7x0

    从文件JsonFile.json中读取并解析Json
    JsonFile.json
    {
      "birthday": "1999-09-29",
      "major": [
        "软件工程",
        "通信工程"
      ],
      "car": null,
      "school": "西安交通大学",
      "hasGirlfriend": false,
      "name": "张飞",
      "comment": "",
      "house": null,
      "age": 25
    }
    
    
    ReadJsonSample.java
    package com.huan.json;
    
    import net.sf.json.JSON;
    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import org.apache.commons.io.FileUtils;
    
    import java.io.File;
    import java.io.IOException;
    
    /**
     * Created by pc on 2017/5/8.
     */
    public class ReadJsonSample {
        public static void main(String[] args) throws IOException {
            readJson();
        }
        public static void readJson() throws IOException {
            //读取JSON文件
            File file = new File(ReadJsonSample.class.getResource("/JsonFile.json").getFile());
                String content = FileUtils.readFileToString(file,"utf-8");
            JSONObject jsonObject = JSONObject.fromObject(content);
            System.out.println("name:"+jsonObject.getString("name"));
            System.out.println("age:"+jsonObject.getString("age"));
            System.out.println("comment:"+jsonObject.getString("comment"));
            System.out.println("hasGirlfriend:"+jsonObject.getString("hasGirlfriend"));
            System.out.println("school:"+jsonObject.getString("school"));
            System.out.println("car:"+jsonObject.getString("car"));
            System.out.println("birthday:"+jsonObject.getString("birthday"));
            JSONArray major= jsonObject.getJSONArray("major");
            for(int i = 0; i<major.length();i++){
                String m = (String) major.get(1);
                System.out.println("专业-"+(i+1)+m);
            }
        }
    }
    

    相关文章

      网友评论

      本文标题:JSON简单快速入门

      本文链接:https://www.haomeiwen.com/subject/ecvutxtx.html