0

I want take some data from a JSON, but the gson is giving an error. I want to know how can i solve this?

I'm using dagger2(this part is 100% ok and working) and rxJava(this part is calling always the onError method).

My model:

@Data
@NoArgsConstructor
public class GradeModel {

//I'm using lombok to generate getters and setters.

    @SerializedName("grade")
    @Expose
    private DecimalFormat grades;

    @SerializedName("grades")
    @Expose
    private GradeModel listGrades;

    private String matter;
    private int fault;

}

My JSON:

grades: [

    {
        "class": "math"
        "grade": 4.5,
        "next_dates":[
            {
                "exam": "03/03/2017",
                "homework": "03/03/2017"
            }

        ]

    }

    {
        "class": "port"
        "grade": 8,
        "next_dates":[
            {
                "exam": "03/05/2017",
                "homework": "03/03/2017"
            }

        ]

    }

]

My Response:

public class GradeResponse {

    @Inject
    Retrofit retrofit;

    @Inject
    MainPresenter mainPresenter;

    public void getGradeRx() {

        MyApplication.getMainComponent().injectIntoGradeResponse(this);// informando ao dagger sobre o uso de um component e a necessidade de injetar dependência

        Subscription getGrade = retrofit
                .create(GradeService.class)
                .getGrade()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .map(model -> {
                    // transform model
                        return model.getListGrades();
                })
                .subscribe(new Observer<GradeModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i(TAG, "saporra vai me matar ainda");
                    }

                    @Override
                    public void onNext(GradeModel grades) {
                        Log.i(TAG, "caralhoooooo " + grades.getGrades());
                        //mainPresenter.setListGrades(grades);
                    }
                });
    }
}

My retrofit:

@Module
public class RetrofitModule {
    @Provides
    @PerActivity
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        //gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
        return gsonBuilder.create();
    }
    @Provides
    @PerActivity
    public OkHttpClient provideHttpClient() {
        return new OkHttpClient().newBuilder().build();
    }
    @Provides
    @PerActivity
    public Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                //converts Retrofit response into Observable
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit;
    }
}

My Service:

public interface GradeService {

    @GET("grades_json.json")
    Observable<GradeModel> getGrade();
}

The Error:

com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $

9
  • Remove grades: from the JSON and it is correct Commented Mar 31, 2017 at 20:33
  • Without remove grades from the JSON has something that i could do? Commented Mar 31, 2017 at 20:35
  • Why can you not edit grades_json.json? It is not proper JSON. Commented Mar 31, 2017 at 20:37
  • You're missing some commas and JSON always has quoted keys for any value. jsonlint.com Commented Mar 31, 2017 at 20:39
  • Because is not my file, i copied that json and put online without the "grades:" and still the same onError method, nothing changed. Commented Mar 31, 2017 at 20:41

1 Answer 1

1

1) grades: [ is not part of valid JSON. Remove grades: and leave the [ if you want to parse a JSON array.

Then, your Gson model is wrong. You have "grades" and "grade", not "grade", "next_dates", and "class".

This is also two objects.

{                    // One Object here
    "class": "math", 
    "grade": 4.5,
    "next_dates":[
        {            // Second object here
            "exam": "03/03/2017",
            "homework": "03/03/2017"
        }
    ]
}

So, your Java classes would be something like

public class GradeDate {
    private String exam, homework;
}

public class GradeModel {
    private String class;
    private Double grade;
    private List<GradeDate> nextDates;
}

Then, your JSON is still missing some commas to separate the values in the outer array.

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.