预期一个字符串,但是BEGIN_ARRAY- Gson

您好我想解析JSON从这个API https://restcountries.eu。但是,当我特里分析topLevelDomain我得到了错误:“E /错误:java.lang.IllegalStateException:期望一个字符串,但BEGIN_ARRAY在第1列42路径$ [0] .topLevelDomain” 如何我可以修复它吗? 在此先感谢预期一个字符串,但是BEGIN_ARRAY- Gson

下面是JSON结构,我的模型,并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;

}

}

MainActivity

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。如果你需要它是Serializable使用ArrayList代替

public class ModelJsona { 

private String flag;

private String name;

private String region;

private List<String> topLevelDomain;

...

}

:你应该把它映射为如此。

以上是 预期一个字符串,但是BEGIN_ARRAY- Gson 的全部内容, 来源链接: utcz.com/qa/267134.html

回到顶部