6

*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 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.