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

问题描述 投票:0回答:2

*我收到错误: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 文件。

android gson
2个回答
11
投票

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

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

   // Getters, Setters
}

并通过GSON解析

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

您可以阅读有关 Gson 的更多信息:Gson 示例


0
投票

4:17

267KB/秒三

66%

डायलबदलें

सिंक्रोनाइज्डडायल

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:अपेक्षितBEGIN_OBJECTलेकिनलाइन1कॉलम58पथपरSTRINGथ $.त्रुटि

© www.soinside.com 2019 - 2024. All rights reserved.