使用RxJava和改造格式错误的JSON

时间:2017-03-31 20:32:21

标签: java android gson rx-java rx-android

我想从JSON中获取一些数据,但是gson发出错误。我想知道我该如何解决这个问题?

我使用dagger2(这部分100%正常并且正常工作)和rxJava(这部分总是调用onError方法)。

我的模特:

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

}

我的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"
            }

        ]

    }

]

我的回复:

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

我的改造:

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

我的服务:

public interface GradeService {

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

错误:

  

com.google.gson.stream.MalformedJsonException:使用   JsonReader.setLenient(true)接受第1行的格式错误的JSON   1路$

1 个答案:

答案 0 :(得分:1)

1)grades: [不是有效JSON的一部分。如果要解析JSON 数组,请移除grades:并离开[

然后,你的Gson模型是错误的。您有"grades""grade",而不是"grade""next_dates""class"

这也是两个对象。

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

因此,您的Java类将类似于

public class GradeDate {
    private String exam, homework;
}

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

然后,您的JSON仍然缺少一些逗号来分隔外部数组中的值。