Невозможно получить значение JSONArray без JSONObject

Я получаю массив JSON от сервера. Если процесс успешен, он дает результат как таковой:

{
    trainingset: [{
        id: "27",
        trainingID: "94",
        studentID: "4",
        trainingsetCreateTime: "1464028658",
        trainingsetFinishTime: "2018/05/10"
    }],
    success_trainingset: 1,
    trainingobject: [{
        trainingobjectID: "1165",
        trainingID: "94",
        trainingobjectLevel: "1",
        trainingobjectAnswer: "69",
        trainingobjectOne: "69",
        trainingobjectTwo: "131",
        trainingobjectThree: null,
        trainingobjectFour: null,
        trainingobjectFive: null
    }, {
        trainingobjectID: "1166",
        trainingID: "94",
        trainingobjectLevel: "1",
        trainingobjectAnswer: "120",
        trainingobjectOne: "120",
        trainingobjectTwo: "129",
        trainingobjectThree: null,
        trainingobjectFour: null,
        trainingobjectFive: null
    }, {
        trainingobjectID: "1167",
        trainingID: "94",
        trainingobjectLevel: "1",
        trainingobjectAnswer: "128",
        trainingobjectOne: "64",
        trainingobjectTwo: "128",
        trainingobjectThree: null,
        trainingobjectFour: null,
        trainingobjectFive: null
    }, {
        trainingobjectID: "1168",
        trainingID: "94",
        trainingobjectLevel: "2",
        trainingobjectAnswer: "63",
        trainingobjectOne: "63",
        trainingobjectTwo: "129",
        trainingobjectThree: "132",
        trainingobjectFour: null,
        trainingobjectFive: null
    }, {
        trainingobjectID: "1169",
        trainingID: "94",
        trainingobjectLevel: "2",
        trainingobjectAnswer: "111",
        trainingobjectOne: "60",
        trainingobjectTwo: "111",
        trainingobjectThree: "130",
        trainingobjectFour: null,
        trainingobjectFive: null
    }, {
        trainingobjectID: "1170",
        trainingID: "94",
        trainingobjectLevel: "2",
        trainingobjectAnswer: "129",
        trainingobjectOne: "111",
        trainingobjectTwo: "129",
        trainingobjectThree: "132",
        trainingobjectFour: null,
        trainingobjectFive: null
    }, {
        trainingobjectID: "1171",
        trainingID: "94",
        trainingobjectLevel: "3",
        trainingobjectAnswer: "69",
        trainingobjectOne: "69",
        trainingobjectTwo: "112",
        trainingobjectThree: "129",
        trainingobjectFour: "132",
        trainingobjectFive: null
    }, {
        trainingobjectID: "1172",
        trainingID: "94",
        trainingobjectLevel: "3",
        trainingobjectAnswer: "112",
        trainingobjectOne: "63",
        trainingobjectTwo: "112",
        trainingobjectThree: "130",
        trainingobjectFour: "131",
        trainingobjectFive: null
    }, {
        trainingobjectID: "1173",
        trainingID: "94",
        trainingobjectLevel: "3",
        trainingobjectAnswer: "129",
        trainingobjectOne: "65",
        trainingobjectTwo: "118",
        trainingobjectThree: "129",
        trainingobjectFour: "132",
        trainingobjectFive: null
    }, {
        trainingobjectID: "1174",
        trainingID: "94",
        trainingobjectLevel: "3",
        trainingobjectAnswer: "131",
        trainingobjectOne: "68",
        trainingobjectTwo: "120",
        trainingobjectThree: "130",
        trainingobjectFour: "131",
        trainingobjectFive: null
    }],
    success_trainingobject: 1,
    training: [{
        trainingID: "94",
        trainingEvaluation: "6",
        trainingAim: "Şekil Eğitimi",
        trainingHood: "test için update",
        trainingExplanation: "test içinıpdate",
        behaviorID: "1",
        trainingTotalQuestion: "10",
        trainingOK: "1",
        trainingCreateTime: "1464028529"
    }],
    success_training: 1,
    objectobject: [{
        objectID: "191",
        objectName: "Yıldız",
        objectImage: "4564-89.png",
        objectNumber: "1",
        shapeID: "11",
        colorID: "18",
        createTime: "1460857130",
    }],
    success_object: 1,
    shape: [{
        shapeID: "11",
        shapeName: "Yıldız"
    }],
    success_shape: 1,
    color: [{
        colorID: "18",
        colorName: "Yeşil"
    }],
    success_color: 1
}

и некоторые другие элементы, как это, и каждый из них имеет успех.

Если success_trainingset не 1, сервер не получит все данные, поэтому мне нужно проверить success_trainingset:. Если это не успешно, выходные данные только:

{
    success_trainingset: 0,
    message: "No trainingset found"
}

Я попробовал все методы JSON, но не получилось.

try {
    jsonObj = new JSONObject(result);//result is string coming from server
}

Это не работает, студия дает красный

jsonObj.getJSONObject("success_trainingset").toString());

Также для этого:

jsonObj.getJSONArray("success_trainingset").getJSONObject(0));

Также добавление toString()по-прежнему сохраняет красный.

Ошибка:

не удается разрешить метод java.ленг.строка

Я искал его, но нашел сообщения с различными проблемами, т. е. не могу решить fromJson

Мой JSON неправильно отформатирован?

I need to check like that:

if (....get("success_trainingset") == 1) { // Or equals
    // Get all
} else {
    // Give error
}

1 ответ

  1. Использование org.codehaus.выбрасывание.формат JSON.JSONObject

    после создания экземпляра JSONObject попробуйте использовать get

    boolean success = new JSONObject(result).get("success_trainingset").toString().equals("1");