2017-12-27 446 views
0

您好我想解析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()); 
     } 
    }); 
} 
+0

帶有諸如https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop等插件的網絡瀏覽器可用於驗證您點擊的服務器是否處於預期格式。發生你以xml格式給出的鏈接 – Pomagranite

回答

0

topLevelDomain是一個數組,而不是String。如果你需要它是Serializable使用ArrayList代替

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

:你應該把它映射爲如此。

相關問題