Java常见应用——Json,二维码生成,加密解密应用
一.JSON
1.下载json架包,json官网:json.org
2.将Map/JavaBean/String转化成json对象,只需要使用JSONObject的构造方法即可
package com.nike.app.json;
import com.nike.app.reflect.Address;
import com.nike.app.reflect.Student;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class Demo01 {
/**
* Map集合转Json对象
*/
public static void mapToJson(){
Map<String,String> map = new HashMap<>();
map.put("name","zhangsan");
map.put("age","22");
map.put("gender","man");
//map->json
JSONObject mjson = new JSONObject(map);
System.out.println(mjson);
//{"gender":"man","name":"zhangsan","age":"22"}
}
public static void beanToJson(){
Student stu = new Student("NikeHu",99,new Address("陕西省","西安市","高新区","高新四路"));
JSONObject bjson = new JSONObject(stu);
System.out.println(bjson);
//{"score":99,"address":{"area":"高新区","province":"陕西省","city":"西安市","street":"高新四路"},"name":"NikeHu"}
}
public static void strToJson(){
String str = "{\"score\":99,\"address\":{\"area\":\"高新区\",\"province\":\"陕西省\",\"city\":\"西安市\",\"street\":\"高新四路\"},\"name\":\"NikeHu\"}";
JSONObject sjson = new JSONObject(str);
System.out.println(sjson);
//{"score":99,"address":{"area":"高新区","province":"陕西省","city":"西安市","street":"高新四路"},"name":"NikeHu"}
}
public static void main(String[] args) {
strToJson();
}
}
3.读取文件,转化为Json对象
package com.nike.app.json;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
* 读取json文件,并将其转化为json对象
*/
public class Demo02 {
public void demo01() throws IOException {
InputStream input = super.getClass().getClassLoader().getResourceAsStream("com/nike/app/json/per.json");
byte[] buf = new byte[10];
int len = -1;
StringBuffer sb = new StringBuffer();
while ((len=input.read(buf))!=-1){
sb.append(new String(buf,0,len));
}
JSONObject jsonObject = new JSONObject(sb.toString());
System.out.println(jsonObject);
//{"address":{"schooladdress":"shanghai","homeaddress":"xian"},"gender":"man","name":"zhangsan","tel":"15682341831","age":22}
}
/**
* 使用commons-io包将文件转化成字符串
*/
public static void demo02() throws IOException {
String str = FileUtils.readFileToString(new File("E:\\IdeaProjects\\JavaApp\\src\\com\\nike\\app\\json\\per.json"), "UTF-8");
JSONObject jobj = new JSONObject(str);
System.out.println(jobj);
//{"address":{"schooladdress":"shanghai","homeaddress":"xian"},"gender":"man","name":"zhangsan","tel":"15682341831","age":22}
}
public static void main(String[] args) {
Demo02 demo = new Demo02();
try {
demo.demo01();
demo02();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.生成json文件
package com.nike.app.json;
import com.nike.app.reflect.Address;
import com.nike.app.reflect.Student;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Demo03 {
public static void jsonToFile() throws IOException {
Student stu01 = new Student("NikeHu",99,new Address("陕西省","西安市","高新区","高新四路"));
Student stu02 = new Student("Mike",77,new Address("陕西省","西安市","高新区","高新四路"));
Student stu03 = new Student("Alex",88,new Address("陕西省","西安市","高新区","高新四路"));
Map<String,Student> stus = new HashMap<>();
stus.put("nike",stu01);
stus.put("Mike",stu02);
stus.put("Alex",stu03);
JSONObject bjson = new JSONObject(stus);
System.out.println(bjson)