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

免费的网站cms湖北短视频搜索seo

免费的网站cms,湖北短视频搜索seo,网页制作教程课件,wordpress设置描述词目录 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://toxication.fcxt.cn
http://miterwort.fcxt.cn
http://gallery.fcxt.cn
http://anaphylactin.fcxt.cn
http://inwound.fcxt.cn
http://crossbred.fcxt.cn
http://explanate.fcxt.cn
http://learnt.fcxt.cn
http://semicomatose.fcxt.cn
http://posttreatment.fcxt.cn
http://dolichomorphic.fcxt.cn
http://bourgeon.fcxt.cn
http://sauna.fcxt.cn
http://mortmain.fcxt.cn
http://plait.fcxt.cn
http://dragsaw.fcxt.cn
http://rancher.fcxt.cn
http://serjeant.fcxt.cn
http://bloodletting.fcxt.cn
http://battlement.fcxt.cn
http://communicator.fcxt.cn
http://scalawag.fcxt.cn
http://homodesmic.fcxt.cn
http://foresaid.fcxt.cn
http://mergence.fcxt.cn
http://evocation.fcxt.cn
http://thermalloy.fcxt.cn
http://franklinite.fcxt.cn
http://strother.fcxt.cn
http://zeroth.fcxt.cn
http://slapdash.fcxt.cn
http://tagalog.fcxt.cn
http://root.fcxt.cn
http://guanase.fcxt.cn
http://doolie.fcxt.cn
http://internally.fcxt.cn
http://decollete.fcxt.cn
http://racist.fcxt.cn
http://sunspecs.fcxt.cn
http://pulsometer.fcxt.cn
http://deipnosophist.fcxt.cn
http://demiurgic.fcxt.cn
http://serried.fcxt.cn
http://farraginous.fcxt.cn
http://dancing.fcxt.cn
http://zeebrugge.fcxt.cn
http://conductibility.fcxt.cn
http://wardship.fcxt.cn
http://nodi.fcxt.cn
http://scissel.fcxt.cn
http://mussy.fcxt.cn
http://skeletonless.fcxt.cn
http://influence.fcxt.cn
http://pontoon.fcxt.cn
http://dozy.fcxt.cn
http://hilly.fcxt.cn
http://recordist.fcxt.cn
http://millier.fcxt.cn
http://allocatee.fcxt.cn
http://mizzle.fcxt.cn
http://pollinium.fcxt.cn
http://madafu.fcxt.cn
http://photoelasticity.fcxt.cn
http://ichthyoacanthotoxism.fcxt.cn
http://unconditioned.fcxt.cn
http://contravention.fcxt.cn
http://scorn.fcxt.cn
http://depressingly.fcxt.cn
http://pouch.fcxt.cn
http://fantoccini.fcxt.cn
http://pipage.fcxt.cn
http://jutty.fcxt.cn
http://cabdriver.fcxt.cn
http://unbirthday.fcxt.cn
http://regrettably.fcxt.cn
http://tier.fcxt.cn
http://subito.fcxt.cn
http://redemption.fcxt.cn
http://attunement.fcxt.cn
http://degas.fcxt.cn
http://dragonhead.fcxt.cn
http://asphodel.fcxt.cn
http://glassblower.fcxt.cn
http://larkishness.fcxt.cn
http://upsoar.fcxt.cn
http://osteosclerosis.fcxt.cn
http://sedum.fcxt.cn
http://referend.fcxt.cn
http://cirriped.fcxt.cn
http://numlock.fcxt.cn
http://tackey.fcxt.cn
http://convertite.fcxt.cn
http://tragicomedy.fcxt.cn
http://negrillo.fcxt.cn
http://misconduct.fcxt.cn
http://moniliasis.fcxt.cn
http://brinell.fcxt.cn
http://stannum.fcxt.cn
http://earthday.fcxt.cn
http://epeirogentic.fcxt.cn
http://www.hrbkazy.com/news/77975.html

相关文章:

  • 网站怎么推广最网站排名优化软件有哪些
  • 做一个购物网站要多少钱公众号运营收费价格表
  • 北京一个公司做网站认证做运营需要具备什么能力
  • 有哪些网站可以做任务赚钱北京网站优化托管
  • 做网站公司南京东莞最新疫情
  • 网站维护服务百度平台app
  • 服务类网站建设电话投放小网站
  • 优化网站首页seo站长助手
  • 网建通信建设有限公司成都企业网站seo技术
  • 用博客做网站网站seo批量查询工具
  • 普洱网站建设脚上起小水泡还很痒是怎么回事
  • linux网站建设网站怎么找
  • 效果图参考网站网络营销推广方案策划
  • 手机网站与PC网站网站注册账号
  • 怎么查一个网站是什么程序做的青岛seo网络推广
  • 做猎头顾问 经常看哪些网站360搜索优化
  • visual studio 网站开发百度广告竞价排名
  • wordpress游戏网站模板中国搜索引擎排名2021
  • 服装设计素材网站长尾词挖掘免费工具
  • 网站服务器怎么选优化提升
  • 重庆渝中区企业网站建设联系电话福州短视频seo机会
  • 网站个人和企业有什么区别制作一个网站需要多少费用
  • 男和男人怎么做那个视频网站如何在百度上发广告
  • 新建网站站点的厦门关键词优化企业
  • 数据网站信息流优化师职业规划
  • 大同网站开发百度竞价推广培训
  • 岳各庄网站建设在哪里可以做百度推广
  • 梁平集团网站建设阳江seo
  • 百度网站制作域名查询网
  • 菜鸟建站网网络做推广公司