【问题标题】:Malformed JSON and “Expected BEGIN_OBJECT but was STRING at line 1 column 1” Error格式错误的 JSON 和“预期为 BEGIN_OBJECT 但在第 1 行第 1 列为 STRING”错误
【发布时间】:2017-05-24 02:54:03
【问题描述】:

更新: 我现在确定问题出在我的改造电话上,它正在检索错误的数据。我正在使用 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, } ] }

改造代码

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

}

【问题讨论】:

  • 请查看question
  • 最后还有一个不需要的逗号:"__v": 0,
  • 你能展示你的Result类吗?
  • 谢谢@dat3450,不幸的是,它没有用。
  • 你的改造调用应该返回一个 Retrieve 实例来匹配上面的 JSON,而不是一个 Result 实例。另外,您确定您发布的 JSON 与 Retrofit 实际从您的服务器检索到的相匹配吗?

标签: java android json parsing gson


【解决方案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
    }
  ]
}

【讨论】:

  • @matthew 对你有帮助吗?
【解决方案2】:

找到了我的解决方案!

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

我在改造调用中添加了端口号 3000:

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

我让 Express 使用

监听特定的 IP

app.listen(portNum, insertIP).

【讨论】:

    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 2015-10-04
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多