gpt4 book ai didi

java - 使用GSON库解析后如何处理json元素?

转载 作者:行者123 更新时间:2023-12-02 01:37:18 27 4
gpt4 key购买 nike

我已将以下有效负载设置到 GsonJsonObject 中:

 {
"backup": {
"addresses": [
"127.0.0.1"
],
"healthcheck_interval": "60",
"http": "https",
"port": "9001"
},
"cognito": {
"addresses": [
"127.0.0.1"
],
"http": "https",
"port": "9012"
},
"collector": {
"addresses": [
"127.0.0.1"
],
"healthcheck_interval": "60",
"http": "https",
"port": "9000"
},
"ds": {
"addresses": [
"127.0.0.1"
],
"http": "https",
"port": "9011"
},
"insight-analytics": {
"addresses": [
"127.0.0.1"
],
"http": "https",
"port": "9013"
},
"integrations": {
"addresses": [
"127.0.0.1"
],
"http": "https",
"port": "9014"
},
"server": {
"addresses": [
"127.0.0.1"
],
"healthcheck_interval": "60",
"http": "https",
"port": "9009"
},
"vigile": {
"addresses": [
"127.0.0.1"
],
"http": "https",
"port": "9443"
},
"walt": {
"addresses": [
"127.0.0.1"
],
"http": "https",
"port": "9010"
}
}

测试:

@Test(priority = 10)
public void parseServicesFileToGsonObject() {
JsonParser jsonParser = new JsonParser();
JsonElement rootNode = jsonParser.parse(response);
if (rootNode.isJsonObject()) {
JsonObject details = rootNode.getAsJsonObject();
}
}

我希望能够将 JSON 的所有成员存储到字符串数组中(backupcognitocollector 等),我可以使用字符串数组找到对象以获取更多值(例如 healthcheck_intervalhttpport等)。我尝试使用流来实现此目的,但没有成功。我想这样做,因为将来该字段的名称很可能会更改(而不是 backup 它可以更改为另一个名称)。

最佳答案

最通用的方法是使用 Map<String, Object>表示 JSON 的动态结构有效负载。在这种情况下,我们总是可以使用 Map#keySet() 方法列出所有属性,我们不需要任何 String[]留住他们。此外,通过键查找对象比使用 array 更快。或List 。如果我们的对象值具有常量结构,我们可以将其映射到 POJO在运行时稳定地访问值。下面的代码显示了这两种情况:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

public class GsonApp {

public static void main(String[] args) throws Exception {
File jsonFile = new File("./resource/test.json").getAbsoluteFile();

Gson gson = new GsonBuilder().create();

System.out.println("Use generic Map");
Type genericMapType = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> map = gson.fromJson(new FileReader(jsonFile), genericMapType);
map.forEach((k, v) -> System.out.println(k + " => " + v));

System.out.println();
System.out.println();

System.out.println("Use Service Map");
Type serviceMapType = new TypeToken<Map<String, Service>>() {
}.getType();
Map<String, Service> serviceMap = gson.fromJson(new FileReader(jsonFile), serviceMapType);
serviceMap.forEach((k, v) -> System.out.println(k + " => " + v));
}
}

class Service {
private List<String> addresses;

@SerializedName("healthcheck_interval")
private int healthCheckInterval;
private String http;
private int port;

public List<String> getAddresses() {
return addresses;
}

public void setAddresses(List<String> addresses) {
this.addresses = addresses;
}

public int getHealthCheckInterval() {
return healthCheckInterval;
}

public void setHealthCheckInterval(int healthCheckInterval) {
this.healthCheckInterval = healthCheckInterval;
}

public String getHttp() {
return http;
}

public void setHttp(String http) {
this.http = http;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

@Override
public String toString() {
return "Service{" +
"addresses=" + addresses +
", healthCheckInterval=" + healthCheckInterval +
", http='" + http + '\'' +
", port=" + port +
'}';
}
}

上面的代码打印:

Use generic Map
backup => {addresses=[127.0.0.1], healthcheck_interval=60, http=https, port=9001}
cognito => {addresses=[127.0.0.1], http=https, port=9012}
collector => {addresses=[127.0.0.1], healthcheck_interval=60, http=https, port=9000}
ds => {addresses=[127.0.0.1], http=https, port=9011}
insight-analytics => {addresses=[127.0.0.1], http=https, port=9013}
integrations => {addresses=[127.0.0.1], http=https, port=9014}
server => {addresses=[127.0.0.1], healthcheck_interval=60, http=https, port=9009}
vigile => {addresses=[127.0.0.1], http=https, port=9443}
walt => {addresses=[127.0.0.1], http=https, port=9010}


Use Service Map
backup => Service{addresses=[127.0.0.1], healthCheckInterval=60, http='https', port=9001}
cognito => Service{addresses=[127.0.0.1], healthCheckInterval=0, http='https', port=9012}
collector => Service{addresses=[127.0.0.1], healthCheckInterval=60, http='https', port=9000}
ds => Service{addresses=[127.0.0.1], healthCheckInterval=0, http='https', port=9011}
insight-analytics => Service{addresses=[127.0.0.1], healthCheckInterval=0, http='https', port=9013}
integrations => Service{addresses=[127.0.0.1], healthCheckInterval=0, http='https', port=9014}
server => Service{addresses=[127.0.0.1], healthCheckInterval=60, http='https', port=9009}
vigile => Service{addresses=[127.0.0.1], healthCheckInterval=0, http='https', port=9443}
walt => Service{addresses=[127.0.0.1], healthCheckInterval=0, http='https', port=9010}

关于java - 使用GSON库解析后如何处理json元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com