【问题标题】:Error parsing nested JSON using Moshi使用 Moshi 解析嵌套 JSON 时出错
【发布时间】:2018-12-25 19:50:18
【问题描述】:

收到错误

com.squareup.moshi.JsonDataException:应为字符串,但路径 $ 处为 BEGIN_OBJECT

正在解析的 JSON

缩短以使其更易于阅读

{
    "success": true,
    "error": null,
    "data": {
        "review_on_me": [
            {
                "request_id": "51",
                "review_from": "9",
                "reviewer": {
                    "id": 9,
                    "first_name": "abc",
                    "device_meta": {
                        "device_type": "iphone",
                        "device_id": "abc"
                    },
                    "location": {
                        "latitude": "43.881731",
                        "longitude": "-79.444322"
                    }
                }
            }
    ]
}

模型类

public class ResponseGetReviews
 {
    @Json(name = "success")
    private Boolean success;
    @Json(name = "error")
    private String error;
    @Json(name = "data")
    private ReviewDataModel data;
}

正在使用 JSON 适配器

其中几个用于所有后续数据对象。这是为了展示他们的样子

public class ResponseGetReviewsAdapter extends JsonAdapter<ResponseGetReviews>
{
    @Override
    public ResponseGetReviews fromJson(JsonReader reader) throws IOException
    {
        Moshi moshi = new Moshi.Builder().build();
        JsonAdapter<ResponseGetReviews> jsonAdapter = moshi.adapter(ResponseGetReviews.class);

        return jsonAdapter.fromJson(reader.nextString());
    }

    @Override
    public void toJson(JsonWriter writer, ResponseGetReviews value) throws IOException
    {
        Moshi moshi = new Moshi.Builder().build();
        JsonAdapter<ResponseGetReviews> jsonAdapter = moshi.adapter(ResponseGetReviews.class);

        writer.value(jsonAdapter.toJson(value));
    }
}

函数调用

Moshi moshi = new Moshi.Builder()
                .add(ResponseGetReviews.class, new ResponseGetReviewsAdapter())
                .add(ReviewDataModel.class, new ReviewDataModelAdapter())
                .add(ReviewDetailModel.class, new ReviewDetailModelAdapter())
                .add(UserModel.class, new UserModelAdapter())
                .add(LocationModel.class, new LocationModelAdapter())
                .add(DeviceMetaModel.class, new DeviceMetaModelAdapter())
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RestAPI.ENDPOINT)
                .addConverterFactory(MoshiConverterFactory.create(moshi))
                .build();
        RestAPI restApi = retrofit.create(RestAPI.class);
        Call<ResponseGetReviews> getReviewsCall = restApi.getClientReviews(auth, clientId);

【问题讨论】:

  • 无需在适配器中创建新的 Moshi 实例。这就是 JsonAdapter.Factory 的用途。

标签: java android json retrofit2 moshi


【解决方案1】:

问题在于您的return jsonAdapter.fromJson(reader.nextString());。通过这种方式,您是说您想从 JSON 中读取 String,但由于您位于对象的开头(这就是错误消息中的 $ 的含义),您不需要有一个字符串,而是一个对象。

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2021-02-03
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-24
    相关资源
    最近更新 更多