Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

gson

*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.

like image 292
Jeeten Parmar Avatar asked Jan 27 '14 06:01

Jeeten Parmar


1 Answers

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

like image 119
Luc Avatar answered Oct 08 '22 21:10

Luc