简体   繁体   中英

Malformed JSON and “Expected BEGIN_OBJECT but was STRING at line 1 column 1” Error

UPDATE: I am now sure that the problem is my retrofit call, it was retrieving the wrong data. I am testing the app with an android device, so localhost won't work. In my retrofit call, I've tried using my public ip and wifi ip (found using by seeing the properties of the wifi I'm connected to), yet the retrofit call is unable to get my JSON. Any ideas how to fix this?

For some reason, my JSON is considered as malformed by GSON, hence I used Gson.setLenient() .

I checked my JSON with jslint and it is valid. In addition, I'm unsure why the first line is considered to be a string when it starts with { .

Why is my JSON malformed and why is it throwing this error?

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

}

Your JSON was actually Malformed at the last element of JsonObject you have placed a comma which is malformed JSON look at code below I corrected it

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

Found my solution!

It was both an android and Express (server side languange) problem.

I added the port number, 3000 to the retrofit call:

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

and I made Express listen to a specific ip using

app.listen(portNum, insertIP).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM