package com.JN.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.json.JSONObject;

import static java.lang.System.out;

/**
 * @ClassName JSONHelper
 * @Description JSON字符串工具类
 * @Author JN
 * @Date 2024/1/12 8:07
 * @Version 1.0
 */
@Slf4j
public class JSONHelper {
    /**
     * @title FormartJson
     * @description 获取JSON中对应key的Value值
     * @author JN
     * @param: jsonString
     * @param: key
     * @updateTime 2024/1/12 8:08
     * @return: java.lang.String
     */
    public static String formartJson(String jsonString, String key) throws JSONException {
        JSONObject jObject = new JSONObject(jsonString);
        return jObject.get(key).toString();
    }

    /**
     * @title PrettyPrintJson
     * @description 控制台和日志打印格式化的JSON字符串
     * @author JN
     * @param: jsonString
     * @updateTime 2024/1/12 8:08
     */
    public static void prettyPrintJson(String jsonString) throws JSONException {
        try {
            ObjectMapper mapper = new ObjectMapper();
            Object obj = mapper.readValue(jsonString, Object.class);
            String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            out.println(indented);
            log.info("Qcc result tokenJson: {}", indented);

        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
