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

时间:2017-12-27 19:25:48

标签: android gson

您好我正试图从这个api https://restcountries.eu解析Json。但是,当我试图解析 topLevelDomain 时,我得到了错误:“E /错误:java.lang.IllegalStateException:期望一个字符串,但是在第1行第42行路径$ B的BEGIN_ARRAY ] .topLevelDomain“ 我该如何解决? 提前致谢

下面是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;
}

}

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());
        }
    });
}

1 个答案:

答案 0 :(得分:0)

topLevelDomain是一个数组,而不是String。您应该将其映射为:

public class ModelJsona {
    private String flag;
    private String name;
    private String region;
    private List<String> topLevelDomain;
    ...
}

如果您需要Serializable使用ArrayList代替。