期待一个字符串,但是BEGIN_ARRAY- Gson
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了期待一个字符串,但是BEGIN_ARRAY- Gson相关的知识,希望对你有一定的参考价值。
嗨,我想从这个api https://restcountries.eu解析Json。但是,当我试图解析topLevelDomain时,我得到了错误:“E /错误:java.lang.IllegalStateException:期望一个字符串,但在第1行第42列路径为$ [0] .topLevelDomain”BEGIN_ARRAY“我该如何修复它?提前致谢
下面是Json结构,My Model和MainActivity
Json结构
[{
"name": "Colombia",
"topLevelDomain": [".co"],
"alpha2Code": "CO",
"alpha3Code": "COL",
"callingCodes": ["57"],
"capital": "Bogotá",
"altSpellings": ["CO", "Republic of Colombia", "República de Colombia"],
"region": "Americas",
"subregion": "South America",
"population": 48759958,
"latlng": [4.0, -72.0],
"demonym": "Colombian",
"area": 1141748.0,
"gini": 55.9,
"timezones": ["UTC-05:00"],
"borders": ["BRA", "ECU", "PAN", "PER", "VEN"],
"nativeName": "Colombia",
"numericCode": "170",
"currencies": [{
"code": "COP",
"name": "Colombian peso",
"symbol": "$"
}],
模型
public class ModelJsona {
private String flag;
private String name;
private String region;
private String topLevelDomain;
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getTopLevelDomain() {
return topLevelDomain;
}
public void setTopLevelDomain(String region) {
this.topLevelDomain = region;
}
}
主要信息
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private DataAdapter dataAdapter;
private List<ModelJsona> dataArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
private void initViews(){
recyclerView=(RecyclerView) findViewById(R.id.card_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
loadJSON();
}
private void loadJSON(){
dataArrayList = new ArrayList<>();
Retrofit retrofit=new Retrofit.Builder().baseUrl("https://restcountries.eu/").addConverterFactory(GsonConverterFactory.create()).build();
RequestInterface requestInterface=retrofit.create(RequestInterface.class);
Call<List<ModelJsona>> call= requestInterface.getJSON();
call.enqueue(new Callback<List<ModelJsona>>() {
@Override
public void onResponse(Call<List<ModelJsona>> call, Response<List<ModelJsona>> response) {
dataArrayList = response.body();
dataAdapter=new DataAdapter(getApplicationContext(),dataArrayList);
recyclerView.setAdapter(dataAdapter);
}
@Override
public void onFailure(Call<List<ModelJsona>> call, Throwable t) {
Log.e("Error",t.getMessage());
}
});
}
topLevelDomain
是一个阵列,而不是String
。您应该将其映射为:
public class ModelJsona {
private String flag;
private String name;
private String region;
private List<String> topLevelDomain;
...
}
如果你需要它是Serializable
而不是使用ArrayList
。
以上是关于期待一个字符串,但是BEGIN_ARRAY- Gson的主要内容,如果未能解决你的问题,请参考以下文章
text 预计BEGIN_OBJECT但是BEGIN_ARRAY
GSON投掷预期的BEGIN_OBJECT但是BEGIN_ARRAY
将 JSON 字符串转换为对象返回“预期的 BEGIN_ARRAY 但为字符串”[重复]
java.lang.illegalstateException:预期BEGIN_OBJECT但是BEGIN_ARRAY Retrofit
错误消息:应为 BEGIN_ARRAY 但是来自我自己的休息服务器 Localhost 的 BEGIN_OBJECT
我怎样才能得到 Gson 的解析?我尝试这样做,但我得到了一个 Expected BEGIN_ARRAY 但是是 BEGIN_OBJECT