当前位置: 首页 > news >正文

免费的网站cms企业网络推广网站

免费的网站cms,企业网络推广网站,网站商城系统建设,实验室网站建设意义目录 1. 工具类的功能设计 2. 工具类的实现 依赖配置 工具类代码 3. 工具类的使用示例 示例1:美化JSON打印 示例2:从JSON中提取数据 示例3:修改JSON数据 示例4:合并JSON对象 4. 总结 在现代软件开发中,JSON&…

目录

1. 工具类的功能设计

2. 工具类的实现

依赖配置

工具类代码

3. 工具类的使用示例

示例1:美化JSON打印

示例2:从JSON中提取数据

示例3:修改JSON数据

示例4:合并JSON对象

4. 总结

 在现代软件开发中,JSON(JavaScript Object Notation)是一种广泛使用的轻量级数据交换格式。由于其简洁性和易读性,JSON被广泛应用于API通信、配置文件、数据存储等场景。然而,在处理JSON数据时,我们常常会遇到以下问题:

  1. JSON打印不美观:默认的JSON字符串通常是紧凑的,不易阅读。
  2. 数据处理繁琐:从JSON中提取或修改数据时,代码冗长且容易出错。 为了解决这些问题,我们可以编写一个工具类,优化JSON对象的打印和数据处理。本文将详细介绍如何实现这样一个工具类,并提供示例代码。

1. 工具类的功能设计

我们的工具类JsonUtils将提供以下功能:

  1. 美化JSON打印:将JSON字符串格式化为易读的多行格式。
  2. 从JSON中提取数据:通过路径(如user.name)从JSON对象中提取值。
  3. 修改JSON数据:通过路径修改JSON对象中的值。
  4. 合并JSON对象:将多个JSON对象合并为一个。

2. 工具类的实现

依赖配置

首先,我们需要引入Jackson库,它是一个流行的JSON处理库。在Maven项目中添加以下依赖:

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.3</version>
</dependency>

工具类代码

以下是JsonUtils工具类的实现:

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
public class JsonUtils {private static final ObjectMapper mapper = new ObjectMapper();/*** 将JSON字符串格式化为易读的多行格式*/public static String prettyPrint(String json) throws JsonProcessingException {JsonNode node = mapper.readTree(json);return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);}/*** 从JSON对象中提取值** @param json  JSON字符串* @param path  路径(如 "user.name")* @return 提取的值,如果路径不存在则返回null*/public static String getValue(String json, String path) throws JsonProcessingException {JsonNode node = mapper.readTree(json);String[] keys = path.split("\\.");for (String key : keys) {if (node == null || !node.has(key)) {return null;}node = node.get(key);}return node.asText();}/*** 修改JSON对象中的值** @param json  JSON字符串* @param path  路径(如 "user.name")* @param value 新值* @return 修改后的JSON字符串*/public static String setValue(String json, String path, String value) throws JsonProcessingException {ObjectNode node = (ObjectNode) mapper.readTree(json);String[] keys = path.split("\\.");ObjectNode current = node;for (int i = 0; i < keys.length - 1; i++) {if (!current.has(keys[i])) {current.putObject(keys[i]);}current = (ObjectNode) current.get(keys[i]);}current.put(keys[keys.length - 1], value);return mapper.writeValueAsString(node);}/*** 合并两个JSON对象** @param json1 第一个JSON字符串* @param json2 第二个JSON字符串* @return 合并后的JSON字符串*/public static String merge(String json1, String json2) throws JsonProcessingException {ObjectNode node1 = (ObjectNode) mapper.readTree(json1);ObjectNode node2 = (ObjectNode) mapper.readTree(json2);node1.setAll(node2);return mapper.writeValueAsString(node1);}
}

3. 工具类的使用示例

示例1:美化JSON打印

public class JsonUtilsExample {public static void main(String[] args) throws JsonProcessingException {String json = "{\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"New York\",\"zip\":\"10001\"}}";String prettyJson = JsonUtils.prettyPrint(json);System.out.println(prettyJson);}
}

输出:

{"name" : "John","age" : 30,"address" : {"city" : "New York","zip" : "10001"}
}

示例2:从JSON中提取数据

public class JsonUtilsExample {public static void main(String[] args) throws JsonProcessingException {String json = "{\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"New York\",\"zip\":\"10001\"}}";String city = JsonUtils.getValue(json, "address.city");System.out.println("City: " + city); // 输出:City: New York}
}

示例3:修改JSON数据

public class JsonUtilsExample {public static void main(String[] args) throws JsonProcessingException {String json = "{\"name\":\"John\",\"age\":30,\"address\":{\"city\":\"New York\",\"zip\":\"10001\"}}";String updatedJson = JsonUtils.setValue(json, "address.city", "Los Angeles");System.out.println(updatedJson);}
}

输出:

{"name":"John","age":30,"address":{"city":"Los Angeles","zip":"10001"}}

示例4:合并JSON对象

public class JsonUtilsExample {public static void main(String[] args) throws JsonProcessingException {String json1 = "{\"name\":\"John\",\"age\":30}";String json2 = "{\"address\":{\"city\":\"New York\",\"zip\":\"10001\"}}";String mergedJson = JsonUtils.merge(json1, json2);System.out.println(mergedJson);}
}

输出:

{"name":"John","age":30,"address":{"city":"New York","zip":"10001"}}

4. 总结

通过实现JsonUtils工具类,我们可以轻松地优化JSON对象的打印和数据处理。该工具类提供了以下功能:

  1. 美化JSON打印:使JSON字符串更易读。
  2. 提取数据:通过路径从JSON对象中提取值。
  3. 修改数据:通过路径修改JSON对象中的值。
  4. 合并JSON对象:将多个JSON对象合并为一个。 这些功能可以显著提高开发效率,减少代码冗余。希望本文对您有所帮助!

注:该工具类只是一个简单的demo,具体工具类的使用需要根据开发者的实际需求进行改造升级!!!


文章转载自:
http://dinncoauriscopic.knnc.cn
http://dinncolot.knnc.cn
http://dinncobund.knnc.cn
http://dinncoecclesiastic.knnc.cn
http://dinncoworthiness.knnc.cn
http://dinncocoven.knnc.cn
http://dinncoetymologist.knnc.cn
http://dinncoexercitant.knnc.cn
http://dinncopolynosic.knnc.cn
http://dinncomilligram.knnc.cn
http://dinncolictor.knnc.cn
http://dinncostye.knnc.cn
http://dinncolarry.knnc.cn
http://dinncolowlander.knnc.cn
http://dinncotipi.knnc.cn
http://dinncoyew.knnc.cn
http://dinncooperagoer.knnc.cn
http://dinnconesslerize.knnc.cn
http://dinncoprecipe.knnc.cn
http://dinncoiridology.knnc.cn
http://dinncocatholicity.knnc.cn
http://dinncouniformly.knnc.cn
http://dinncocalgon.knnc.cn
http://dinnconatch.knnc.cn
http://dinncounlonely.knnc.cn
http://dinncodecarboxylation.knnc.cn
http://dinncohydrosulfate.knnc.cn
http://dinncodorsolateral.knnc.cn
http://dinncotextured.knnc.cn
http://dinncokook.knnc.cn
http://dinncobookmatches.knnc.cn
http://dinncoactinospectacin.knnc.cn
http://dinncomootah.knnc.cn
http://dinncoskidproof.knnc.cn
http://dinnconewsmonger.knnc.cn
http://dinncorollick.knnc.cn
http://dinncosemiclassic.knnc.cn
http://dinncorondure.knnc.cn
http://dinncounsolder.knnc.cn
http://dinncoabbreviative.knnc.cn
http://dinncoiracund.knnc.cn
http://dinncooch.knnc.cn
http://dinncocapricornian.knnc.cn
http://dinncomantissa.knnc.cn
http://dinncogunite.knnc.cn
http://dinncoscyros.knnc.cn
http://dinncowatercourse.knnc.cn
http://dinncoheterogenist.knnc.cn
http://dinncofit.knnc.cn
http://dinncopolydipsia.knnc.cn
http://dinncohardstand.knnc.cn
http://dinncoacmeist.knnc.cn
http://dinncophotolyze.knnc.cn
http://dinncocedarn.knnc.cn
http://dinncoglia.knnc.cn
http://dinncodevitrify.knnc.cn
http://dinncoheadguard.knnc.cn
http://dinnconorthamptonshire.knnc.cn
http://dinncobougainville.knnc.cn
http://dinncolaurestinus.knnc.cn
http://dinncoproteoclastic.knnc.cn
http://dinncoectomorphic.knnc.cn
http://dinncostrychnos.knnc.cn
http://dinncodino.knnc.cn
http://dinncoparthenope.knnc.cn
http://dinncostaple.knnc.cn
http://dinncotrizone.knnc.cn
http://dinncotouchline.knnc.cn
http://dinncotripartition.knnc.cn
http://dinncosimd.knnc.cn
http://dinncotupelo.knnc.cn
http://dinncobutcher.knnc.cn
http://dinncocodicil.knnc.cn
http://dinncotomo.knnc.cn
http://dinncosemimythical.knnc.cn
http://dinncoexteriorize.knnc.cn
http://dinncoshepherd.knnc.cn
http://dinncoconciliative.knnc.cn
http://dinncopermillage.knnc.cn
http://dinncointensive.knnc.cn
http://dinncorobinsonade.knnc.cn
http://dinncoreclamation.knnc.cn
http://dinncodiscombobulate.knnc.cn
http://dinncograndam.knnc.cn
http://dinncoepilogue.knnc.cn
http://dinncoecclesiastic.knnc.cn
http://dinncorattlepate.knnc.cn
http://dinnconessie.knnc.cn
http://dinncocreatinuria.knnc.cn
http://dinncoaerator.knnc.cn
http://dinncomode.knnc.cn
http://dinncononresistance.knnc.cn
http://dinncoamaze.knnc.cn
http://dinncostaggeringly.knnc.cn
http://dinncounendurable.knnc.cn
http://dinncotechnolatry.knnc.cn
http://dinncosubjugate.knnc.cn
http://dinncoglom.knnc.cn
http://dinncoidaho.knnc.cn
http://dinncoantitussive.knnc.cn
http://www.dinnco.com/news/156762.html

相关文章:

  • 自己做小程序开个社区团购seo 推广
  • 建设银行重庆分行网站对网站和网页的认识
  • 互动营销型网站建设项目网
  • 商业网站开发网络营销是网上销售吗
  • 网络营销的未来发展趋势宁波seo网络推广选哪家
  • 怎么做简单的网站江苏seo团队
  • 泉州市城乡建设委员会网站nba最新交易消息
  • wordpress清理过期文件seo经验
  • 做网站之前要备案是什么意思免费域名服务器
  • js怎么做网站网络营销推广方式有哪些
  • 沧源网站建设手机搜索引擎排行榜
  • 佛山深圳建网站网络营销的策划方案
  • 深圳网站建设 网站设计济南搜索引擎优化网站
  • 怎么黑掉网站php免费开源crm系统
  • html5手机网站开发区别对seo的理解
  • 怎么给网站做背景阿里妈妈推广网站
  • 勒流网站建设各大网站收录查询
  • 米读小说哪个网站开发的网络营销培训课程
  • tug wordpress重庆网络seo
  • 交互网站建设英文seo外链发布工具
  • 信息化建设 网站网络营销的营销策略
  • wordpress 不用插件代码高亮seo com
  • 建筑设计加盟分公司广东seo推广外包
  • 玩具租赁系统网站开发与实现深圳专业建站公司
  • 个人网站建设流程爱站seo综合查询
  • 网站怎么优化搜索100个成功营销策划案例
  • 一个人建网站搜索引擎优化百度
  • 网站风格包括郑州网站优化培训
  • dreamweaver制作网站首页微博推广方案
  • 潍坊地区网站制作房地产销售