在Android中读取Json数组时出错

时间:2018-04-29 23:28:46

标签: java android arrays json android-studio

JSON

{
"data": [{
    "id": "0bfe9470-4a57-11e8-8594-9fed6c4fe65b",
    "type": 6,
    "order": 12,
    "data": {
        "goto": "+",
        "type": 6,
        "order": 12,
        "options": [{
            "display": "P\u00e9ssimo"
        }, {
            "display": "Ruim"
        }, {
            "display": "Regular"
        }, {
            "display": "Bom"
        }, {
            "display": "\u00d3timo"
        }],
        "required": true,
        "answer": []
    }
}, {
    "id": "0594b030-4a57-11e8-8c83-0d7c6461d799",
    "type": 5,
    "order": 10,
    "data": {
        "type": 5,
        "order": 10,
        "options": {
            "left": {
                "goto": "+",
                "display": 0
            },
            "right": {
                "goto": "+",
                "display": 100
            },
            "center": {
                "goto": "+",
                "display": 50
            }
        },
        "required": true,
        "answer": []
    }
}, {
    "id": "00f8dea0-4a57-11e8-ab0b-7b5c52826681",
    "type": 4,
    "order": 9,
    "data": {
        "type": 4,
        "order": 9,
        "options": [{
            "items": [{
                "goto": "+",
                "display": "Bom"
            }, {
                "goto": "+",
                "display": "Regular"
            }, {
                "goto": "+",
                "display": "Ruim"
            }],
            "display": "Thiago Silva"
        }, {
            "items": [{
                "goto": "+",
                "display": "Bom"
            }, {
                "goto": "+",
                "display": "Regular"
            }, {
                "goto": "+",
                "display": "Ruim"
            }],
            "display": "Thiago Silva"
        }],
        "required": true,
        "answer": []
    }
}]
}

CODE - PageReports ADD

public class PageReports implements Serializable{

    @SerializedName("data")
    @Expose
    private List<Datum> data = null;

    public List<Datum> getData() {
        return data;
    }

    public void setData(List<Datum> data) {
        this.data = data;
    }

}

private void getReportPages(String id) {
    if (subscription != null) {
        subscription.unsubscribe();
    }
    subscription = RxUtil.appleHandlerStartFinish(
            RestBuilder.api().getReportPage(RestParams.authHeaderAccept(getContext()), id),
            () -> base.displayProcess(),
            () -> base.dismissProcess())
            .compose(RxUtil.applySchedulers())
            .subscribe(
                    pageReports -> {
                       // LogUtil.e("JSON: ", SuApp.getInstance().gson().toJson(pageReports));
                        if (pageReports != null && pageReports.getData() != null && pageReports.getData().size() > 0) {
                            currentDataReport.setDataPages(pageReports.getData());
                            viewPager.setOffscreenPageLimit(pageReports.getData().size());
                            mPagerAdapter = new ReportPagerAdapter(getChildFragmentManager(), pageReports.getData());
                            viewPager.setAdapter(mPagerAdapter);
                            indicator.setViewPager(viewPager);
                        }
                    },
                    throwable -> throwable.printStackTrace()
            );
}

public static Map<String,String> authHeaderAccept(Context context) {
    Map<String, String> header = defaultHeader();
    header.put("Authorization","Bearer " + Preference.getInstance(context).getToken());
    return header;
}

ERROR

W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 125 path $.data[0].data.options
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
                  at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
                  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
                  at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
                  at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:39)
                  at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27)
                  at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:122)
                  at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:217)
W/System.err:     at retrofit2.OkHttpCall.execute(OkHttpCall.java:180)
                  at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:40)
                  at retrofit2.adapter.rxjava.CallExecuteOnSubscribe.call(CallExecuteOnSubscribe.java:24)
                  at retrofit2.adapter.rxjava.BodyOnSubscribe.call(BodyOnSubscribe.java:36)
                  at retrofit2.adapter.rxjava.BodyOnSubscribe.call(BodyOnSubscribe.java:28)
                  at rx.Observable.unsafeSubscribe(Observable.java:10256)
                  at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100)
                  at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:230)
                  at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
              Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 125 path $.data[0].data.options
W/System.err:     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
                  at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
                ... 28 more

我试图读但总是错误,如果我在JSON中只有一个值我可以进行搜索,但是当它有3时它不会。

好像GSON()类没有转换。在这种情况下可以做些什么,以便不再发生错误?更改JSON还是我尚未识别的代码中有错误?

1 个答案:

答案 0 :(得分:0)

您的第二个data对象在[ ]以及options后遗失{/ 1}}:

"options": {
        "left": {
            "goto": "+",
无论出于何种原因......

制作本

"options": [{
            "left": {
                "goto": "+",

..并在末尾关闭带有相应数组的数组