一、maven相关设置
1、下载apache-maven-3.6.1,
2、解压repo放入到maven中
3、在apache-maven-3.6.1/conf替换settings,然后设置为自己的镜像仓库
二、创建maven项目
1、点击File->New->Project
2、选择Maven中的安装的jdk,后一直下一步
3、然后点击File->Settings-Maven, 设置Maven home path、User settings file、Local respository
4、为了避免每次新建项目需要重复设置的问题,可以File->New Projects Setup->setttings for New Projects
二、Java对象、jsonStr、Map、json转换
package com.lemon.json; public class User { //json转java对象,java对象必须有私有属性,对应的get/set方法,空参。 //属性的名称必须和json的key保持一致 private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '/'' + ", password='" + password + '/'' + '}'; } }
package com.lemon.json; public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '/'' + ", age=" + age + '}'; } }
package com.lemon.json; import com.alibaba.fastjson.JSONObject; import java.util.List; import java.util.Map; public class JsonDemo { public static void main(String[] args) { //1、 parseObject(json字符串,需要转成的java类.class) jsonStr =>java对象 String jsonStr = "{/"username/": /"ZHANGSAN/", /"password/": /"123456/"}"; User user = JSONObject.parseObject(jsonStr, User.class); System.out.println(user); System.out.println(user.getUsername()); System.out.println(user.getPassword()); System.out.println("===============1、jsonStr =>java对象==========="); //2、java对象=>jsonStr String s = JSONObject.toJSONString(user); System.out.println(s); System.out.println("===============2、java对象=>jsonStr==========="); //3、 Map 天生就能和json互转 Map<String, Object> map = JSONObject.parseObject(jsonStr, Map.class); System.out.println(map.get("username")); String s1 = JSONObject.toJSONString(map); System.out.println(s1); System.out.println("=============== 3、 Map 天生就能和json互转==========="); //4、json数组 =>java对象 String jsonStr2 = "[{/"name/": /"张三/", /"age/": 18},{/"name/": /"李四/", /"age/": 16}]"; List<Student> students = JSONObject.parseArray(jsonStr2, Student.class); System.out.println(students); System.out.println("=============== 4、json数组 =>java对象==========="); } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288848.html