Lỗi lấy dữ liệu từ JSon - Android

Chào mọi người . Mình muốn lấy dữ liệu từ file Json như thế này :

Nhưng bị báo lỗi là Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path

Đây là code của mình.

public static List<Notification> bindNotifyData(List<JsonElement> list)
{
    List<Notification> results= new ArrayList<>();

    for(JsonElement item:list)
    {
        JsonElement data = item.getAsJsonObject().get("updates");
        JsonObject dataJsonObj = data.getAsJsonObject();
        JsonArray notifyJsonArray = dataJson.get("updates").getAsJsonArray();

        //ArrayList<Notification> notifyList = new ArrayList<>();
        if(notifyJsonArray != null && notifyArray.size() > 0) {
            for(int i = 0; i < notifyJsonArray.size(); i++) {
                JsonObject notifyJson = (JsonObject) notifyJsonArray.get(i);
                Notification notification = new Notification();
                notification.setContent(notifyJson.get("content").getAsString());
                notification.setTime(notifyJson.get("time").getAsString());
                // priceHtml.setExtras(priceHtmlJSon.get("extras").getAsString());
                results.add(notification);
            }
        }
    }

    return results;
}

M.n có thể xem và giúp mình được không ạ . Cảm ơn m.n nhiều

rõ ràng data là JsonArray, sao lại có dòng JsonObject dataJsonObj = data.getAsJsonObject();?
Thử bỏ dòng đấy, dòng dưới thay bằng JsonArray notifyJsonArray = data.getAsJsonArray(); xem?

1 Like

Vẫn lỗi bạn ơi :sleepy::sleepy:

Bạn làm ngay một khóa ngắn hạn về json để biết cái nào là array cái nào là object. Đọc kĩ java doc về các method sử dụng. Chứ code có 1 mẩu không đầy đủ, mà chắc gì lỗi nó ở cái đoạn post lên, người muốn giúp bạn đọc cũng không dò ra lỗi được.

2 Likes

Đây là hàm Retrofit của mình.

// NOTIFICATION
@Headers("Content-Type: application/json")
@GET("track")
Call<List<JsonElement>> getNotify();

Hàm này lấy data Json vào list.

private void fetchNotifyData() {
    Call<List<JsonElement>> callGetNotify = apiService.getNotify();
    assert mainActivity.loadingView != null;
    mainActivity.loadingView.setVisibility(View.VISIBLE);
    callGetNotify.enqueue(new Callback<List<JsonElement>>() {
        @Override
        public void onResponse(Call<List<JsonElement>> call, Response<List<JsonElement>> response) {
            int code = response.code();
            if (code == HttpURLConnection.HTTP_OK) {
                isDataLoaded = true;

                List<JsonElement> body = response.body();
                if(body != null && body.size() > 0) {
                    notifyList = BindFetchDataHelper.bindNotifyData(body);
                    initNotifyCoursesLayout();
                }
                else {
                    notifyLayout.setVisibility(View.VISIBLE);
                }
            }

            mainActivity.loadingView.setVisibility(View.GONE);
        }
    }
}

M.n giúp mình với ạ

sai logic ngay từ cái đoạn khai báo retrofit interface rồi thì sửa ở dưới kiểu gì cũng lỗi. Response trả về là 1 json object nhưng bạn lại đi khai báo nhận về 1 json array thì chả lỗi.
Cái đoạn List tương tự như 1 json array rồi nhé.
PS: Mà lưu ý k post ảnh code lên nhé, khi nãy có 1 bạn tốt bụng convert từ ảnh code sang code text cho bạn rồi, nhưng lần sau k nên như thế

2 Likes

bạn chỉ rõ hơn để mình hiểu được không? Tại mới học cũng còn mông lung quá

như mình đã bảo bên trên. Bạn nên đọc kĩ về spec của JSON để hiểu được JSON gồm những gì và các khái niệm của nó. Ngoài ra bạn nên xem lại kiến thức java của mình vì mình thấy bạn hơi hổng, việc mới học mà đã nhảy quá nhanh nên thành ra không thể tự sửa được lỗi.

Mình sẽ giải thích phần lỗi mà bạn mắc phải khi khai báo retrofit interface

Call<List<JsonElement>> getNotify();

Đoạn code này sẽ khai báo rằng API này sẽ trả về 1 json array. Tức là file json của bạn phải ở dạng

[ // <--- đây là json array
    {
        ....
    }
]

trong khi file json của bạn lại là

{ // <--- đây là json object

}

Hai dấu [] và {} là khác nhau đấy bạn. Vì thế khai báo của bạn là k đúng. Thay vì khai báo Call<List<JsonElement>> getNotify(); thì phải là Call<JsonObject> getNotify();

Mình làm được r nhé, thanks you so much

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?