Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to quickly parse the helper class of an element in an json string

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to quickly parse the help class of the elements in the json string, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

During the development process, I came across a lot of requirements for parsing json strings, taking the elements corresponding to the requirements. In the process, I wrote a helper class that can quickly fetch json strings.

First, introduce dependency: / / manage implementation "com.fasterxml.jackson.core:jackson-databind" with gradle. Second, help class code: 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 () / * parse json strings according to the path to obtain String type data * * @ 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 }} / * parse the json string according to the path to get the Integer type string * * @ 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 }} / * parse the json string according to the path, and get the json string * * @ 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 3. Use:

If json parsing requirements are heavily used in project development, you can use this help class.

The above is how to quickly parse the help class of the elements in the json string. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report