怎么能夠快速解析json字符串中元素的幫助類

本篇文章為大家展示了怎么能夠快速解析json字符串中元素的幫助類,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

創(chuàng)新互聯(lián)建站專注于江南企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站,成都商城網(wǎng)站開(kāi)發(fā)。江南網(wǎng)站建設(shè)公司,為江南等地區(qū)提供建站服務(wù)。全流程按需規(guī)劃網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)

在開(kāi)發(fā)過(guò)程中,我大量接觸到了解析json字符串的需求,拿去對(duì)應(yīng)需求的元素。在此過(guò)程中,我編寫(xiě)了能夠快速拿取json字符串的幫助類。

一、引入依賴:

//用gradle管理
implementation "com.fasterxml.jackson.core:jackson-databind"

二、幫助類代碼:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

/**
 * @author edison_kwok
 */
public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 根據(jù)路徑解析json字符串,獲取String類型數(shù)據(jù)
     *
     * @param content
     * @param path
     * @return
     * @throws IOException
     */
    public static String parse(String content, Object[] path) throws IOException {
        if (StringUtils.isBlank(content)) {
            return null;
        }
        try {
            JsonNode node = objectMapper.readTree(content);
            int len = path.length;
            int index = 0;
            while (index < len) {
                if (path[index] instanceof Integer) {
                    int i = (Integer) path[index];
                    node = node.get(i);
                } else if (path[index] instanceof String) {
                    String s = (String) path[index];
                    node = node.get(s);
                }
                index++;
            }
            String result = node.toString();
            return result.substring(1, result.length() - 1);
        } catch (NullPointerException e) {
            return null;
        }
    }

    /**
     * 根據(jù)路徑解析json字符串,獲取Integer類型字符串
     *
     * @param content
     * @param path
     * @return
     * @throws IOException
     */
    public static Integer parseInteger(String content, Object[] path) throws IOException {
        if (StringUtils.isBlank(content)) {
            return null;
        }
        try {
            JsonNode node = objectMapper.readTree(content);
            int len = path.length;
            int index = 0;
            while (index < len) {
                if (path[index] instanceof Integer) {
                    int i = (Integer) path[index];
                    node = node.get(i);
                } else if (path[index] instanceof String) {
                    String s = (String) path[index];
                    node = node.get(s);
                }
                index++;
            }
            String result = node.toString();
            return Integer.parseInt(result);
        } catch (NullPointerException e) {
            return null;
        }
    }

    /**
     * 根據(jù)路徑解析json字符串,獲取當(dāng)中的json字符串
     *
     * @param content
     * @param path
     * @return
     * @throws IOException
     */
    public static String parseJson(String content, Object[] path) throws IOException {
        if (StringUtils.isBlank(content)) {
            return null;
        }
        try {
            JsonNode node = objectMapper.readTree(content);
            int len = path.length;
            int index = 0;
            while (index < len) {
                if (path[index] instanceof Integer) {
                    int i = (Integer) path[index];
                    node = node.get(i);
                } else if (path[index] instanceof String) {
                    String s = (String) path[index];
                    node = node.get(s);
                }
                index++;
            }
            return node.toString();
        } catch (NullPointerException e) {
            return null;
        }
    }
}

三、使用:

在項(xiàng)目開(kāi)發(fā)中,如果大量使用到了json解析需求,可以使用本幫助類。

上述內(nèi)容就是怎么能夠快速解析json字符串中元素的幫助類,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文名稱:怎么能夠快速解析json字符串中元素的幫助類
標(biāo)題鏈接:http://bm7419.com/article2/jccpoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)云服務(wù)器、虛擬主機(jī)、外貿(mào)建站自適應(yīng)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)