com.google.gson.JsonSyntaxException:android中的java.lang.IllegalStateException

时间:2014-01-27 06:49:04

标签: android gson

*我收到错误:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期字符串但在第1行第3列是BEGIN_OBJECT *

我的代码:

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

我得到的输出结果如下:

[{"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"}]

请帮我解决这个问题。我已经在配置中添加了gson.jar文件。

1 个答案:

答案 0 :(得分:11)

我认为:(您的JSON不是字符串数组,它是对象数组)

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

   // Getters, Setters
}

由GSON解析

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

您可以在Gson Example

了解更多有关Gson的信息