Java的JSON文件解析

1. JSON格式概述

  • JSON
     JavaScript
     JavaScript Object Notation
     (JavaScript Object Notation,JavaScript对象表示法,读作/ˈdʒeɪsən/)是一种由道格拉斯·克罗克福特构想和设计、轻量级的数据交换语言,该语言以易于让人阅读的文字为基础,用来传输由属性值或者序列性的值组成的数据对象。尽管JSON是JavaScript的一个子集,但JSON是独立于语言的文本格式,并且采用了类似于C语言家族的一些习惯。
 {

{
     "firstName": "John",
     "lastName": "Smith",
     "sex": "male",
     "age": 25,
     "married": false,
     "address": 
     {
         "streetAddress": "21 2nd Street",
         "city": "New York",
         "state": "NY",
         "postalCode": "10021"
     },
     "phoneNumber": 
     [
         {
           "type": "home",
           "number": "212 555-1234"
         },
         {
           "type": "fax",
           "number": "646 555-4567"
         }
     ]
 }

2. 数据格式

2.1 JSON对象
  • 特征:
  1. 数据形式键值对形式“键”:值
  2. 数据支持 字符串,数字,true false
  3. {} 大括号以内的数据
2.2 JSON对象数组
  • 特征:
  1. 数据使用[]包含
  2. 在[]都是JSON格式对象
  3. 每一个对象之间使用逗号隔开,同时最后一个元素不需要逗号
2.3 JSON数据验证

JSON格式验证

2.4 解析JSON格式工具
  • 常用的工具:
    Gson,fastjson, Jackson
    以上都是第三方工具,需要导入对应的jar包按使用
    XML导包
2.5 FastJson内容
  • JSON核心类
    JSON核心类提供解析和转化方法,用于解析JSON数据格式,同时用于转换类对象到JSON格式,该类对象需要符合JavaBean规范
    –| JSONArray
    存在按照键值对方式解析获取数据,同时存在一定的List方法
    –| JSONObject
    获取对应的类对象,指定键值对对应数据的方法
    解析演示
/*
完成一个符合JavaBean规范的类
*/
public class Student {
    private Strint name;
    private Integer age;
    
    // 这里根据需要完成对应的Setter和getter方法
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jdk.nashorn.internal.ir.CallNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Anonymous
 * @description FastJson演示
 * @date 2020/3/9 16:34
 */
public class Demo1 {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();
        Student stu = new Student("骚磊", 16);
        list.add(stu);
        list.add(new Student("骚杰", 66));
        list.add(new Student("老黑", 56));
        list.add(new Student("老付", 36));
        list.add(new Student("老高", 66));

        /*
        JavaBean List ==> Json String
        */
        String s = JSON.toJSONString(list);
        System.out.println(s);

        System.out.println("--------------------------------------");

        /*
        JavaBean Student类对象 ==> Json String
         */
        String s1 = JSON.toJSONString(stu);
        System.out.println(s1);

        /*
        Json String == Java Bean Student
         */
        Student student = JSON.parseObject(s1, Student.class);
        System.out.println(student);


        /*
        Json String ==> Json Object
         */
        JSONObject jsonObject = JSON.parseObject(s1);
        String name = jsonObject.getString("name");
        System.out.println(name);
        System.out.println(jsonObject.getInteger("age"));

        /*
        Json String ==> JsonArray
         */
        JSONArray objects = JSON.parseArray(s);
        System.out.println(objects);

        for (Object object : objects) {
            JSONObject jsonObject1 = (JSONObject) object;

            System.out.println(jsonObject1.getString("name"));
            System.out.println(jsonObject1.getInteger("age"));
        }


        Student object = objects.getObject(2, Student.class);
        System.out.println(object);

        System.out.println("--------------------------------------");
        /*
        JSONArray ==> JavaBean List集合
         */
        List<Student> students = objects.toJavaList(Student.class);
        for (Student student1 : students) {
            System.out.println(student1);
        }

        // StudentMangaer ==> ArrayList ==> JSONString ==> 文件
        // 文件 ==> 程序 ==> JSONString ==> JSONArray ==> ArrayList

        System.out.println("--------------------------------------");
    }
}
发布了23 篇原创文章 · 获赞 32 · 访问量 7783

猜你喜欢

转载自blog.csdn.net/qq_46288317/article/details/104780461