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

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

一、方式一,引用gson工具

测试报文:

java 复制代码
{
	"account":"zhangsan",
	"password":"123456"
}

引入pom

java 复制代码
<!-- 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>

代码

java 复制代码
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

测试报文:

java 复制代码
{
	"account":"zhangsan",
	"password":"123456",
	"flag":"true"
}

代码

java 复制代码
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

测试报文:

java 复制代码
{
	"account":"zhangsan",
	"password":"123456"
}

代码

java 复制代码
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;
        }
        @Override
        public String toString() {
            return "Login{" +
                    "account='" + account + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
}

四、方式四,使用hutool,获取报文数组数据

测试报文:

java 复制代码
{
	"code":"0",
	"message":"",
	"data":[{
		"account":"zhangsan",
		"password":"123456"
	}]
}

引入pom

java 复制代码
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>4.1.19</version>
</dependency>

代码

java 复制代码
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

相关推荐
吾日三省吾码4 小时前
JVM 性能调优
java
Estar.Lee4 小时前
查手机号归属地免费API接口教程
android·网络·后端·网络协议·tcp/ip·oneapi
弗拉唐5 小时前
springBoot,mp,ssm整合案例
java·spring boot·mybatis
oi776 小时前
使用itextpdf进行pdf模版填充中文文本时部分字不显示问题
java·服务器
2401_857610036 小时前
SpringBoot社团管理:安全与维护
spring boot·后端·安全
少说多做3436 小时前
Android 不同情况下使用 runOnUiThread
android·java
知兀6 小时前
Java的方法、基本和引用数据类型
java·笔记·黑马程序员
蓝黑20207 小时前
IntelliJ IDEA常用快捷键
java·ide·intellij-idea
Ysjt | 深7 小时前
C++多线程编程入门教程(优质版)
java·开发语言·jvm·c++