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

雁过留声  金牌会员 | 2024-8-16 18:09:43 | 来自手机 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 507|帖子 507|积分 1521

一、方式一,引用gson工具

测试报文:
  1. {
  2.         "account":"yanxiaosheng",
  3.         "password":"123456"
  4. }
复制代码
引入pom
  1. <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
  2. <dependency>
  3.         <groupId>com.google.code.gson</groupId>
  4.         <artifactId>gson</artifactId>
  5.         <version>2.6.2</version>
  6. </dependency>
复制代码
测试类:
  1. import com.google.gson.JsonElement;
  2. import com.google.gson.JsonObject;
  3. import com.google.gson.JsonParser;
  4. @Test
  5. public void test() throws Exception {
  6.         String json = "{\n" +
  7.                 "\t"account":"yanxiaosheng",\n" +
  8.                 "\t"password":"123456"\n" +
  9.                 "}";
  10.         JsonParser jsonParser = new JsonParser();
  11.         JsonElement jsonElement = jsonParser.parse(json);
  12.         JsonObject jsonObject = jsonElement.getAsJsonObject();
  13.         String fieldValue = jsonObject.get("account").getAsString();
  14.         System.out.println(fieldValue);
  15. }
复制代码

二、方式二,使用jackson 

  1. {
  2.         "account":"yanxiaosheng",
  3.         "password":"123456",
  4.         "flag":"true"
  5. }
复制代码
测试类:
  1. import com.fasterxml.jackson.core.JsonProcessingException;
  2. import com.fasterxml.jackson.databind.JsonNode;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. @Test
  5.     public void test() throws Exception {
  6.         String json = "{\n" +
  7.                 "\t"account":"yanxiaosheng",\n" +
  8.                 "\t"password":"123456",\n" +
  9.                 "\t"flag":"true"\n" +
  10.                 "}";
  11.         ObjectMapper objectMapper = new ObjectMapper();
  12.         JsonNode jsonNode = objectMapper.readTree(json);
  13.         String account = jsonNode.get("account").asText();
  14.         int password = jsonNode.get("password").asInt();
  15.         boolean flag = jsonNode.get("flag").asBoolean();
  16.         System.out.println(account);
  17.         System.out.println(password);
  18.         System.out.println(flag);
  19.     }
复制代码
三、方式三,使用jackson转换Object

测试报文:
  1. {
  2.         "account":"yanxiaosheng",
  3.         "password":"123456"
  4. }
复制代码
测试类:
  1. @Test
  2.     public void test() throws Exception {
  3.         String json = "{\n" +
  4.                 "\t"account":"yanxiaosheng",\n" +
  5.                 "\t"password":"123456"\n" +
  6.                 "}";
  7.         ObjectMapper objectMapper = new ObjectMapper();
  8.         Login login = objectMapper.readValue(json, DepositTest.Login.class);
  9.         System.out.println(login.toString());
  10.     }
  11.     public static class Login{
  12.         private String account;
  13.         private String password;
  14.         public String getAccount() {
  15.             return account;
  16.         }
  17.         public void setAccount(String account) {
  18.             this.account = account;
  19.         }
  20.         public String getPassword() {
  21.             return password;
  22.         }
  23.         public void setPassword(String password) {
  24.             this.password = password;
  25.         }
  26.         @Override
  27.         public String toString() {
  28.             return "Login{" +
  29.                     "account='" + account + '\'' +
  30.                     ", password='" + password + '\'' +
  31.                     '}';
  32.         }
  33.     }
复制代码

 注意!!!DepositTest.Login.class   DepositTest  需使用自己写的测试类名
四、方式四,使用hutool,获取报文数组数据 

测试报文: 
  1. {
  2.         "code":"0",
  3.         "message":"",
  4.         "data":[{
  5.                 "account":"yanxiaosheng",
  6.                 "password":"123456"
  7.         }]
  8. }
复制代码
引入pom
  1. <dependency>
  2.         <groupId>cn.hutool</groupId>
  3.         <artifactId>hutool-all</artifactId>
  4.         <version>4.1.19</version>
  5. </dependency>
复制代码
测试类:
  1.   @Test
  2.     public void test() throws Exception {
  3.         String json = "{\n" +
  4.                 "\t"code":"0",\n" +
  5.                 "\t"message":"",\n" +
  6.                 "\t"data":[{\n" +
  7.                 "\t\t"account":"yanxiaosheng",\n" +
  8.                 "\t\t"password":"123456"\n" +
  9.                 "\t}]\n" +
  10.                 "}";
  11.         JSONObject jsonObject = new JSONObject(json);
  12.         JSONArray jsonArray = jsonObject.getJSONArray("data");
  13.         JSONObject resultObject = jsonArray.getJSONObject(0);
  14.         String account = resultObject.getStr("account");
  15.         System.out.println(account);
  16.     }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

雁过留声

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表