2016-05-25 14 views
0

サーバーからJSON配列を取得しました。処理が成功すると、次のような出力が得られます。JSONObjectなしでJSONArrayの値を取得できません

{ 
    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が間違ってフォーマットしますか?

org.codehaus.jettison.json.JSONObject

を使用して

I need to check like that: 

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

を使用してみてください 'int'とあなたでsuccess_trainingset'を渡しているので、' 'int'を構文解析する方法については、' object'チェック(この回答)(http://stackoverflow.com/a/33034370/2553431) – Mohit

+0

上記のリンクはあなたを助けましたか? – Mohit

答えて

0

あなたはJSONObjectをインスタンス化した後は、取得

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

関連する問題