2

I use a moshi to parse JSon to Object . This is my JSon :

[{
   "id": 4,
   "phone":    {
      "id": 3,
      "imei": "35693803",
      "description": "M Kow"
   },
   "user":    {
      "id": 3,
      "username": "m.kow",
      "first_name": "M",
      "last_name": "Kow"
   },
   "user_id": 3,
   "message": "Podejrzane zachowanie, sprawdĹş.",
   "date_time_data": "2017-09-05T22:26:44.916927Z",
   "longitude": 19.934113,
   "latitude": 50.075086,
   "confirm": 0,
   "changetime": "2017-09-05T22:26:44.930357Z",
   "has_coordinates": 1
}]

This is my Object :

public class Message {

    String message;
    double longitude;
    double latitude;
    int confirm;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public int getConfirm() {
        return confirm;
    }

    public void setConfirm(int confirm) {
        this.confirm = confirm;
    }
}

And I did this to parse JSON to obecjt :

 Message message = App.getMsg().fromJson(new String(responseBody, "UTF-8"));

But my app is crash and in logs I see this :

User-space exception detected! com.squareup.moshi.JsonDataException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at path $ at com.squareup.moshi.BufferedSourceJsonReader.beginObject(BufferedSourceJsonReader.java:171) at com.squareup.moshi.ClassJsonAdapter.fromJson(ClassJsonAdapter.java:145) at com.squareup.moshi.JsonAdapter$1.fromJson(JsonAdapter.java:68) at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:33) at com.squareup.moshi.JsonAdapter.fromJson(JsonAdapter.java:37) at .apk.MsgActivity$1.onSuccess(MsgActivity.java:37) at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:351) at com.loopj.android.http.AsyncHttpResponseHandler$ResponderHandler.handleMessage(AsyncHttpResponseHandler.java:510) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

1
  • 1
    List<Message> message instead of Message message because you have array of objects Commented Sep 9, 2017 at 13:35

1 Answer 1

7

The problem is that you are asking for a Message object but you actually have an array of Message objects. Try doing this:

String jsonResponseBody = ...;
Type type = Types.newParameterizedType(List.class, Message.class);
JsonAdapter<List<Message>> adapter = moshi.adapter(type);
List<Message> messages = adapter.fromJson(jsonResponseBody);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.