更新: 我现在确信问题出在我的改造调用上,它检索了错误的数据。我正在使用 Android 设备测试该应用程序,因此 localhost 将无法工作。在我的改造调用中,我尝试使用我的公共(public) ip 和 wifi ip (通过查看我所连接的 wifi 的属性发现使用),但改造调用无法获取我的 JSON。有什么想法可以解决这个问题吗?
由于某种原因,GSON 认为我的 JSON 格式错误,因此我使用了 Gson.setLenient()
。
我使用 jslint 检查了我的 JSON,它是有效的。另外,我不确定为什么第一行以 {
开头时被认为是 string
。
为什么我的 JSON 格式错误以及为什么会抛出此错误?
JSON
{
“结果”: [
{
"_id": "5924eea5bd50af38702c14ae",
“名称”:“斯托尼面包公司”,
"cuisineType": "意大利",
"地址": "奥克维尔克尔街 325 号",
"开放时间": "0900",
"关闭时间": "2100",
“液化石油气”:43.443733,
“纬度”:-79.68146,
“__v”:0,
}
]
}
改造代码
Gson gson = new GsonBuilder().setLenient().create();
final MapInterface retrofit = new Retrofit.Builder()
.baseUrl("http://myComputerIP/places/")
.addConverterFactory(GsonConverterFactory.create(gson))
.build().create(MapInterface.class);
Call<Result> call = retrofit.getMongoosePlace("3333733", "-79.681460");
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Integer getV() {
return v;
}
public void setV(Integer v) {
this.v = v;
}
}
最佳答案
您的 JSON 实际上在 JsonObject 的最后一个元素处格式错误,您放置了一个逗号,这是格式错误的 JSON,请查看下面的代码,我已更正它
{
"result": [
{
"_id": "5924eea5bd50af38702c14ae",
"name": "Stoney's Bread Company",
"cuisineType": "Italian",
"address": "325 Kerr Street, Oakville",
"openTime": "0900",
"closeTime": "2100",
"lng": 43.443733,
"lat": -79.68146,
"__v": 0
}
]
}
关于java - JSON 格式错误和 “Expected BEGIN_OBJECT but was STRING at line 1 column 1” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44148171/