gpt4 book ai didi

java - 从 json 响应中解析一个值,其中大部分负载都是冗余的

转载 作者:行者123 更新时间:2023-12-04 08:54:46 25 4
gpt4 key购买 nike

提取 name来自这个 json 负载的字段:

[{"name":"Estonia","topLevelDomain":[".ee"],"alpha2Code":"EE","alpha3Code":"EST","callingCodes":["372"],"capital":"Tallinn","altSpellings":["EE","Eesti","Republic of Estonia","Eesti Vabariik"],"region":"Europe","subregion":"Northern Europe","population":1315944,"latlng":[59.0,26.0],"demonym":"Estonian","area":45227.0,"gini":36.0,"timezones":["UTC+02:00"],"borders":["LVA","RUS"],"nativeName":"Eesti","numericCode":"233","currencies":[{"code":"EUR","name":"Euro","symbol":"€"}],"languages":[{"iso639_1":"et","iso639_2":"est","name":"Estonian","nativeName":"eesti"}],"translations":{"de":"Estland","es":"Estonia","fr":"Estonie","ja":"エストニア","it":"Estonia","br":"Estônia","pt":"Estónia","nl":"Estland","hr":"Estonija","fa":"استونی"},"flag":"https://restcountries.eu/data/est.svg","regionalBlocs":[{"acronym":"EU","name":"European Union","otherAcronyms":[],"otherNames":[]}],"cioc":"EST"}]
我计划使用类似于以下内容的代码:
@Service
public class RestService {

private final RestTemplate restTemplate;

public RestService(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}

public String getPostsPlainJSON() {
String url = "hhttps://restcountries.eu/rest/v2/name/eesti";
return this.restTemplate.getForObject(url, MyCustomObject.class);
}
}
MyCustomObject将包含许多我不关心的冗余字段,因为我只想访问名称 "Estonia"对于上面的例子。是否有解析字段的替代方法 name而不是创建一个新的 MyCustomObject映射到整个 JSON 响应的 pojo?

最佳答案

您可以使用 JSONPath

public static void main(String[] args) {
String json = "[{\"name\":\"Estonia\",\"topLevelDomain\":[\".ee\"],\"alpha2Code\":\"EE\",\"alpha3Code\":\"EST\",\"callingCodes\":[\"372\"],\"capital\":\"Tallinn\",\"altSpellings\":[\"EE\",\"Eesti\",\"Republic of Estonia\",\"Eesti Vabariik\"],\"region\":\"Europe\",\"subregion\":\"Northern Europe\",\"population\":1315944,\"latlng\":[59.0,26.0],\"demonym\":\"Estonian\",\"area\":45227.0,\"gini\":36.0,\"timezones\":[\"UTC+02:00\"],\"borders\":[\"LVA\",\"RUS\"],\"nativeName\":\"Eesti\",\"numericCode\":\"233\",\"currencies\":[{\"code\":\"EUR\",\"name\":\"Euro\",\"symbol\":\"€\"}],\"languages\":[{\"iso639_1\":\"et\",\"iso639_2\":\"est\",\"name\":\"Estonian\",\"nativeName\":\"eesti\"}],\"translations\":{\"de\":\"Estland\",\"es\":\"Estonia\",\"fr\":\"Estonie\",\"ja\":\"エストニア\",\"it\":\"Estonia\",\"br\":\"Estônia\",\"pt\":\"Estónia\",\"nl\":\"Estland\",\"hr\":\"Estonija\",\"fa\":\"استونی\"},\"flag\":\"https://restcountries.eu/data/est.svg\",\"regionalBlocs\":[{\"acronym\":\"EU\",\"name\":\"European Union\",\"otherAcronyms\":[],\"otherNames\":[]}],\"cioc\":\"EST\"}]";

String jsonPath = "$.[*].name";
DocumentContext jsonContext = JsonPath.parse(json);
List<String> result = jsonContext.read(jsonPath);
System.out.println("name :: "+result.get(0));

result = jsonContext.read("$.[*].capital"); //To get Captial
System.out.println("Capital :: "+result.get(0));
}
输出
name :: Estonia
Capital :: Tallinn
Maven 依赖
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.4.0</version>
</dependency>

关于java - 从 json 响应中解析一个值,其中大部分负载都是冗余的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63892105/

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