JSON格式验证
1.3 解析JSON格式工具常用的工具: Gson,fastjson, Jackson 以上都是第三方工具,需要导入对应的jar包按使用 XML导包1.3.1 FastJson内容JSON核心类 JSON核心类提供解析和转化方法,用于解析JSON数据格式,同时用于转换类对象到JSON格式,该类对象需要符合JavaBean规范 --| JSONArray 存在按照键值对方式解析获取数据,同时存在一定的List方法 --| JSONObject 获取对应的类对象,指定键值对对应数据的方法1.3.2 解析演示/* 完成一个符合JavaBean规范的类 */ public class Student { private Strint name; private Integer age; // 这里根据需要完成对应的Setter和getter方法 }package com.qfedu.b_json; 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("--------------------------------------"); } } ---来自腾讯云社区的---用户7073689Java知识点——JSON文件---用户7073689
JSON1.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"
}
]
}1.2 数据格式1.2.1 JSON对象{
"ID":001,
"name":"骚磊",
"age":16
}特征:
1. 数据形式键值对形式
"键":值
2. 数据支持 字符串,数字,true false
3. {} 大括号以内的数据1.2.2 JSON对象数组[
{
"ID":1,
"name":"骚磊",
"age":16
},
{
"ID":2,
"name":"骚杰",
"age":66
},
{
"ID":3,
"name":"康康",
"age":15
}
]特征:
1. 数据使用[]包含
2. 在[]都是JSON格式对象
3. 每一个对象之间使用逗号隔开,同时最后一个元素不需要逗号1.2.3 JSON数据验证

微信扫一扫打赏
支付宝扫一扫打赏