java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > JSON字符串获取指定字段值

JSON字符串中获取一个指定字段的值四种方式

作者:闫小生

在Java开发中,我们经常会遇到需要从JSON数据中提取特定字段值的情况,这篇文章主要给大家介绍了关于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;

@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转换Object

测试报文:

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

测试类:

@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 + '\'' +
                    '}';
        }
    }

 注意!!!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>

测试类:

  @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);
    }

总结

到此这篇关于JSON字符串中获取一个指定字段的值四种方式的文章就介绍到这了,更多相关JSON字符串获取指定字段值内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文