JSON字符串中獲取一個(gè)指定字段的值四種方式
一、方式一,引用gson工具
測(cè)試報(bào)文:
{
"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>
測(cè)試類(lèi):
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"
}測(cè)試類(lèi):
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
@Test
public 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轉(zhuǎn)換Object
測(cè)試報(bào)文:
{
"account":"yanxiaosheng",
"password":"123456"
}測(cè)試類(lèi):
@Test
public 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;
}
@Override
public String toString() {
return "Login{" +
"account='" + account + '\'' +
", password='" + password + '\'' +
'}';
}
}
注意!?。?strong>DepositTest.Login.class DepositTest 需使用自己寫(xiě)的測(cè)試類(lèi)名
四、方式四,使用hutool,獲取報(bào)文數(shù)組數(shù)據(jù)
測(cè)試報(bào)文:
{
"code":"0",
"message":"",
"data":[{
"account":"yanxiaosheng",
"password":"123456"
}]
}引入pom
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.1.19</version> </dependency>
測(cè)試類(lèi):
@Test
public 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);
}
總結(jié)
到此這篇關(guān)于JSON字符串中獲取一個(gè)指定字段的值四種方式的文章就介紹到這了,更多相關(guān)JSON字符串獲取指定字段值內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring學(xué)習(xí)通過(guò)AspectJ注解方式實(shí)現(xiàn)AOP操作
這篇文章主要為大家介紹了Spring學(xué)習(xí)通過(guò)AspectJ注解方式實(shí)現(xiàn)AOP操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
使用SpringMVC訪問(wèn)Controller接口返回400BadRequest
這篇文章主要介紹了使用SpringMVC訪問(wèn)Controller接口返回400BadRequest,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
java stream中Collectors的用法實(shí)例精講
這篇文章主要為大家介紹了java stream中Collectors的用法實(shí)例精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Mybatis 實(shí)現(xiàn)動(dòng)態(tài)組裝查詢條件,仿SQL模式
這篇文章主要介紹了Mybatis 實(shí)現(xiàn)動(dòng)態(tài)組裝查詢條件,仿SQL模式的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

