格式错误的JSON和“预期BEGIN_OBJECT但在第1行第1列STRING”错误

时间:2017-05-24 02:54:04

标签: java android json parsing gson

更新 我现在确定问题是我的改装调用,它正在检索错误的数据。我正在使用Android设备测试应用程序,因此localhost将无法正常工作。在我的改装电话中,我尝试使用我的公共IP和wifi ip(通过查看我连接到的wifi的属性找到),但改装调用无法获取我的JSON。任何想法如何解决这个问题?

出于某种原因,我的JSON被GSON视为格式错误,因此我使用了Gson.setLenient()

我用jslint检查了我的JSON,它是有效的。另外,我不确定为什么第一行在以string开头时被视为{

为什么我的JSON格式不正确,为什么会抛出此错误?

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, } ] }

Retrofitcode

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

}

2 个答案:

答案 0 :(得分:1)

你的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
    }
  ]
}

答案 1 :(得分:0)

找到我的解决方案!

这是一个android和Express(服务器端语言)问题。

我在改装电话中添加了端口号3000:

.baseUrl("http://myComputerIP:3000/places/")

我让Express使用

侦听特定的ip

app.listen(portNum, insertIP).