I know, i know
地球另一端有你陪我
一、Maven
Java 作为面向对象的语言,常常使用已存在的现有资源
通常会被打包上传于网上,称为 jar 包
不同 jar 包,可能会依赖于其他的 jar 包,和特定的 java 版本
手动导入和使用会比较不方便
Maven 作为一个工具,第一个功能是管理项目,另一个功能是管理 jar 包
需要修改包的储存位置,和下载的服务器
二、常用 jar 包
1、 Gson & fast
json 是前端常用的一种语言,其特点是内部均为大小嵌套的键值对
为了方便解析这样的键值对,目前有两种常用的 jar 包用来解析
Gson
import com.google.gson.Gson;
import day35.student;
import java.util.HashMap;
public class gsontest {
public static void main(String[] args) {
String json =
"{\"id\":1001,\"name\":\"zs\",\"age\":18,\"sex\":\"男\",\"班级\":\"一班\"}";
System.out.println(json);
//通过Gson对象调用方法
Gson gson = new Gson();
//json转化成对象,通过手写的student类
//fromJson(...,class)
student person = gson.fromJson(json, student.class);
System.out.println(person);
System.out.println(person.getName());
//json转化成对象,通过现有的map类
HashMap hm = gson.fromJson(json, HashMap.class);
System.out.println(hm);
System.out.println(hm.get("id"));
System.out.println("---------------------------");
//对象转化成
// json tojson(...)
String s1 = gson.toJson(hm);
System.out.println(s1);
String s2 = gson.toJson(person);
System.out.println(s2);
//也不是啥都能转
int[] i = {
1,2,3,4,5,6};
String s3 = gson.toJson(i);
System.out.println(s3);
}
}
Fastjson
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import day35.student;
public class Fastjson {
public static void main(String[] args) {
String json =
"{\"id\":1001,\"name\":\"zs\",\"age\":18,\"sex\":\"男\",\"班级\":\"一班\"}";
//json->对象
// 通过反射进行转换 转换成指定对象
//通过set方法
student person = JSON.parseObject(json,student.class);
System.out.println(person);
//json->对象
// 通过反射进行转换 转换成默认JSONObject对象 不需要创新的对象
JSONObject jsonObject = JSON.parseObject(json);
System.out.println(jsonObject);
//对象->json
String s = JSON.toJSONString(person);
System.out.println(s);
}
}
1、 JUnit
在 Java 中,想要测试一个方法至少需要一个 main() 方法
并且手动调用函数
该 jar 包允许方法可以直接测试允许
import org.junit.Test;
import org.junit.After;
import org.junit.Before;
public class testaaa {
@After
//后运行
public void fun(){
System.out.println("111");
}
@Test
public void fun2(){
System.out.println("222");
}
@Before
//先运行
public void fun3(){
System.out.println("333");
}
}
输出结果: 333
222
111
总结
1、Fastjson
在将 json 转为对象的时候,使用的是 set 方法,因此需要添加该方法
2、JUnit
@Test 无法正确带出路径,可能是应为同时存在多个版本的 JUnit.jar
需要手动删除至一个
3、Warning:java: 源值1.5已过时, 将在未来所有发行版中删除
File - Setting - Build - compiler - java compiler
均修改为8