JSON字符串中获取一个特定字段的值
- 一、方式一,引用gson工具
- 二、方式二,使用jackson
- 三、方式三,使用jackson转换Object
- 四、方式四,使用hutool,获取报文数组数据
一、方式一,引用gson工具
测试报文:
{"account":"zhangsan","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>
代码
package com;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;public class Test {public static void main(String[] args) {String json = "{\n" +"\t\"account\":\"zhangsan\",\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":"zhangsan","password":"123456","flag":"true"
}
代码
package com;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;public class Test {public static void main(String[] args) throws JsonProcessingException {String json = "{\n" +"\t\"account\":\"zhangsan\",\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":"zhangsan","password":"123456"
}
代码
package com;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;public class Test {public static void main(String[] args) throws JsonProcessingException {String json = "{\n" +"\t\"account\":\"zhangsan\",\n" +"\t\"password\":\"123456\"\n" +"}";ObjectMapper objectMapper = new ObjectMapper();Login login = objectMapper.readValue(json, Test.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 + '\'' +'}';}}
}
四、方式四,使用hutool,获取报文数组数据
测试报文:
{"code":"0","message":"","data":[{"account":"zhangsan","password":"123456"}]
}
引入pom
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>4.1.19</version>
</dependency>
代码
package com;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;public class Test {public static void main(String[] args) throws JsonProcessingException {String json = "{\n" +"\t\"code\":\"0\",\n" +"\t\"message\":\"\",\n" +"\t\"data\":[{\n" +"\t\t\"account\":\"zhangsan\",\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);}
}
OK,就这些吧。
有什么不对的还望指正,书写不易,觉得有帮助就点个赞吧!☺☺☺
参考链接:https://www.cnblogs.com/xbinbin/p/17647956.html