没有JSONObject

时间:2016-05-25 13:32:56

标签: java json

我从服务器获得了一个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.lang.string

我搜索了它,但找到了有不同问题的帖子,即Can't Resolve fromJson

我的JSON格式错误了吗?

I need to check like that:

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

1 个答案:

答案 0 :(得分:0)

使用org.codehaus.jettison.json.JSONObject

在您实例化JSONObject之后尝试使用get

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