com.google.gson.stream.MalformedJsonException error: How to solve it?
com.google.gson.stream.MalformedJsonException error: How to solve it?
我想从外部 api 获取国家详细信息,并使用 Gson 将从 get 请求接收到的数据设置为 class 国家。问题是在响应中,货币键的值介于 [](请参见下文)之间,在某些情况下,货币名称值之间存在 space,这会导致以下错误
com.google.gson.stream.MalformedJsonException:第 1 行第 41 列路径中的未终止对象 $.currencies[0].name:
"currencies":[{"code":"BGN","name":"Bulgarian lev","symbol":"лв"}]
@RestController
public class CountryController {
@Autowired
private RestTemplate restTemplate;
private static String baseURL = "https://restcountries.com/v2/";
public Object[] getCountryDetails(String countryName){
Object[] countryDetails = restTemplate.getForObject(baseURL+"name/"+countryName+"?fields=name,alpha2Code,alpha3Code,capital,currencies", Object[].class);
return countryDetails;
}
public Country createCountryObject(String countryName) {
String response = Arrays.asList(getCountryDetails(countryName)).get(0).toString();
Gson g = new Gson();
JsonReader reader = new JsonReader(new StringReader(response.trim()));
reader.setLenient(true);
Country country = g.fromJson(reader, Country.class);
return country;
}
@GetMapping("/")
public String getAll(){
Country country = createCountryObject("bulgaria");
return country.getName();
}
}
Country.java:
package country.neighbours.tour.models;
import java.util.List;
public class Country {
private String name;
private String alpha2Code;
private String alpha3Code;
private List<String> borders;
private Object[] currencies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getBorders() {
return borders;
}
public void setBorders(List<String> borders) {
this.borders = borders;
}
public String getAlpha2Code() {
return alpha2Code;
}
public void setAlpha2Code(String alpha2Code) {
this.alpha2Code = alpha2Code;
}
public String getAlpha3Code() {
return alpha3Code;
}
public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}
public Object[] getCurrencies() {
return currencies;
}
public void setCurrencies(Object[] currencies) {
this.currencies = currencies;
}
}
如何只获取货币代码?
看起来您正在解析响应两次;一旦使用 restTemplate.getForObject
,然后将其结果转换为字符串(toString()
调用的结果很可能是 而不是 JSON),然后您尝试用 Gson 再次解析它。
如果您只想使用 Gson,可以在 fromJson
调用中使用 TypeToken
来解析响应 JSON 数组:
List<Country> countries = gson.fromJson(..., new TypeToken<List<Country>>() {}.getType());
也许更熟悉 Spring 的人也可以解释如何只使用 RestTemplate.getForObject
而不是 Gson。
我想从外部 api 获取国家详细信息,并使用 Gson 将从 get 请求接收到的数据设置为 class 国家。问题是在响应中,货币键的值介于 [](请参见下文)之间,在某些情况下,货币名称值之间存在 space,这会导致以下错误 com.google.gson.stream.MalformedJsonException:第 1 行第 41 列路径中的未终止对象 $.currencies[0].name:
"currencies":[{"code":"BGN","name":"Bulgarian lev","symbol":"лв"}]
@RestController
public class CountryController {
@Autowired
private RestTemplate restTemplate;
private static String baseURL = "https://restcountries.com/v2/";
public Object[] getCountryDetails(String countryName){
Object[] countryDetails = restTemplate.getForObject(baseURL+"name/"+countryName+"?fields=name,alpha2Code,alpha3Code,capital,currencies", Object[].class);
return countryDetails;
}
public Country createCountryObject(String countryName) {
String response = Arrays.asList(getCountryDetails(countryName)).get(0).toString();
Gson g = new Gson();
JsonReader reader = new JsonReader(new StringReader(response.trim()));
reader.setLenient(true);
Country country = g.fromJson(reader, Country.class);
return country;
}
@GetMapping("/")
public String getAll(){
Country country = createCountryObject("bulgaria");
return country.getName();
}
}
Country.java:
package country.neighbours.tour.models;
import java.util.List;
public class Country {
private String name;
private String alpha2Code;
private String alpha3Code;
private List<String> borders;
private Object[] currencies;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getBorders() {
return borders;
}
public void setBorders(List<String> borders) {
this.borders = borders;
}
public String getAlpha2Code() {
return alpha2Code;
}
public void setAlpha2Code(String alpha2Code) {
this.alpha2Code = alpha2Code;
}
public String getAlpha3Code() {
return alpha3Code;
}
public void setAlpha3Code(String alpha3Code) {
this.alpha3Code = alpha3Code;
}
public Object[] getCurrencies() {
return currencies;
}
public void setCurrencies(Object[] currencies) {
this.currencies = currencies;
}
}
如何只获取货币代码?
看起来您正在解析响应两次;一旦使用 restTemplate.getForObject
,然后将其结果转换为字符串(toString()
调用的结果很可能是 而不是 JSON),然后您尝试用 Gson 再次解析它。
如果您只想使用 Gson,可以在 fromJson
调用中使用 TypeToken
来解析响应 JSON 数组:
List<Country> countries = gson.fromJson(..., new TypeToken<List<Country>>() {}.getType());
也许更熟悉 Spring 的人也可以解释如何只使用 RestTemplate.getForObject
而不是 Gson。