com.google.gson.JsonSyntaxException: java.lang.IllegalStateException in android

人盡茶涼 提交于 2020-01-03 10:42:47

问题


*I am getting error : com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 3*

My Code:

Gson gson = new Gson();
String[] placelist;
placelist = gson.fromJson(result, String[].class);
// Assign the String array as Country Spinner Control's items
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, placelist);
spinnerFood.setAdapter(adapter);

I am getting output in result which is as below :

[{"CityId":1,"CityName":"Vadodara"},{"CityId":2,"CityName":"ahmedabad"},{"CityId":3,"CityName":"Gandhinagar"},{"CityId":4,"CityName":"Bhavnagar"},{"CityId":15,"CityName":"Anantapur"},{"CityId":16,"CityName":"Srikakulam"},{"CityId":17,"CityName":"Rajahmundry"},{"CityId":18,"CityName":"Guntur"},{"CityId":29,"CityName":"Hyderabad"},{"CityId":30,"CityName":"Karimnagar"}]

Please help me to solve this problem. I have already added gson.jar file in configuration.


回答1:


I think: (Your JSON is not String array, It is Object Array)

public class City {
   private String cityId;
   private String cityName;

   // Getters, Setters
}

And parse by GSON

City[] placelist;
placelist = gson.fromJson(result, City[].class);

You can read more about Gson at: Gson Example



来源:https://stackoverflow.com/questions/21374603/com-google-gson-jsonsyntaxexception-java-lang-illegalstateexception-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!