Java常见应用——Json,二维码生成,加密解密应用

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)
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值