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

JSON字符串中获取一个指定字段的值

一、方式一,引用gson工具

测试报文:

{"account":"yanxiaosheng","password":"123456"
}

引入pom

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency><groupId>com.google.code.gson</groupId><artifactId>gson</artifactId><version>2.6.2</version>
</dependency>

测试类:

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;@Test
public void test() throws Exception {String json = "{\n" +"\t\"account\":\"yanxiaosheng\",\n" +"\t\"password\":\"123456\"\n" +"}";JsonParser jsonParser = new JsonParser();JsonElement jsonElement = jsonParser.parse(json);JsonObject jsonObject = jsonElement.getAsJsonObject();String fieldValue = jsonObject.get("account").getAsString();System.out.println(fieldValue);
}

二、方式二,使用jackson 

{"account":"yanxiaosheng","password":"123456","flag":"true"
}

测试类:


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;@Testpublic void test() throws Exception {String json = "{\n" +"\t\"account\":\"yanxiaosheng\",\n" +"\t\"password\":\"123456\",\n" +"\t\"flag\":\"true\"\n" +"}";ObjectMapper objectMapper = new ObjectMapper();JsonNode jsonNode = objectMapper.readTree(json);String account = jsonNode.get("account").asText();int password = jsonNode.get("password").asInt();boolean flag = jsonNode.get("flag").asBoolean();System.out.println(account);System.out.println(password);System.out.println(flag);}

三、方式三,使用jackson转换Object

测试报文:

{"account":"yanxiaosheng","password":"123456"
}

测试类:

@Testpublic void test() throws Exception {String json = "{\n" +"\t\"account\":\"yanxiaosheng\",\n" +"\t\"password\":\"123456\"\n" +"}";ObjectMapper objectMapper = new ObjectMapper();Login login = objectMapper.readValue(json, DepositTest.Login.class);System.out.println(login.toString());}public static class Login{private String account;private String password;public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "Login{" +"account='" + account + '\'' +", password='" + password + '\'' +'}';}}

 注意!!!DepositTest.Login.class   DepositTest  需使用自己写的测试类名

四、方式四,使用hutool,获取报文数组数据 

测试报文: 

{"code":"0","message":"","data":[{"account":"yanxiaosheng","password":"123456"}]
}

引入pom

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.1.19</version>
</dependency>

测试类:

  @Testpublic void test() throws Exception {String json = "{\n" +"\t\"code\":\"0\",\n" +"\t\"message\":\"\",\n" +"\t\"data\":[{\n" +"\t\t\"account\":\"yanxiaosheng\",\n" +"\t\t\"password\":\"123456\"\n" +"\t}]\n" +"}";JSONObject jsonObject = new JSONObject(json);JSONArray jsonArray = jsonObject.getJSONArray("data");JSONObject resultObject = jsonArray.getJSONObject(0);String account = resultObject.getStr("account");System.out.println(account);}

相关文章:

  • 第1章 人工智能的基础概念与应用导论
  • C++ 定长队列的实现
  • 【博士每天一篇文献-综述】A survey on few-shot class-incremental learning
  • 【已解决】: fatal error: cuda_runtime_api.h: No such file or directory
  • 行业洞察 | 2024应用程序安全领域现状报告
  • Dataweave2 语法教程
  • 【前端项目笔记】8 订单管理
  • python笔记
  • 详细分析Oracle修改默认的时间格式(四种方式)
  • Anaconda创建虚拟环境+Pycharm和Jupyter使用虚拟环境
  • 数据结构--顺序表
  • flink使用StatementSet降低资源浪费
  • 深入探讨JavaScript中的队列,结合leetcode全面解读
  • nvm安装以及idea下vue启动项目过程和注意事项
  • 华为仓颉编程语言
  • 【mysql】环境安装、服务启动、密码设置
  • 【跃迁之路】【733天】程序员高效学习方法论探索系列(实验阶段490-2019.2.23)...
  • 03Go 类型总结
  • Android 控件背景颜色处理
  • angular2 简述
  • avalon2.2的VM生成过程
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • Linux后台研发超实用命令总结
  • PV统计优化设计
  • rc-form之最单纯情况
  • React系列之 Redux 架构模式
  • sessionStorage和localStorage
  • Vue 动态创建 component
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 多线程 start 和 run 方法到底有什么区别?
  • 基于web的全景—— Pannellum小试
  • 基于游标的分页接口实现
  • 经典排序算法及其 Java 实现
  • 如何设计一个比特币钱包服务
  • 双管齐下,VMware的容器新战略
  • 原生 js 实现移动端 Touch 滑动反弹
  • 你对linux中grep命令知道多少?
  • # Python csv、xlsx、json、二进制(MP3) 文件读写基本使用
  • #知识分享#笔记#学习方法
  • $.proxy和$.extend
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (175)FPGA门控时钟技术
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (WSI分类)WSI分类文献小综述 2024
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (亲测有效)推荐2024最新的免费漫画软件app,无广告,聚合全网资源!
  • (一)基于IDEA的JAVA基础12
  • 、写入Shellcode到注册表上线
  • .NET CF命令行调试器MDbg入门(一)
  • .net refrector
  • @RequestBody与@ModelAttribute
  • [1159]adb判断手机屏幕状态并点亮屏幕
  • [BeginCTF]真龙之力