当前位置:   article > 正文

JSON字符串中获取一个指定字段的值_gson获取json某一个数据

gson获取json某一个数据

一、方式一,引用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. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/1012147
推荐阅读
相关标签
  

闽ICP备14008679号