Sagen wir, das ist mein JSON Object
{
"LabelData": {
"slogan": "AWAKEN YOUR SENSES",
"jobsearch": "JOB SEARCH",
"contact": "CONTACT",
"video": "ENCHANTING BEACHSCAPES",
"createprofile": "CREATE PROFILE"
}
}
Ich muss wissen, dass entweder "Video" in diesem Objekt vorhanden ist oder nicht, und wenn es existiert, muss ich den Wert dieses Schlüssels ermitteln. Ich habe folgendes versucht, aber ich kann den Wert dieses Schlüssels nicht ermitteln.
containerObject= new JSONObject(container);
if(containerObject.hasKey("video")){
//get Value of video
}
Verwenden Sie den folgenden Code, um zu finden, dass der Schlüssel vorhanden ist oder nicht in JsonObject
. Die has("key")
-Methode wird verwendet, um Schlüssel in JsonObject
zu finden.
containerObject= new JSONObject(container);
//has method
if(containerObject.has("video")){
//get Value of video
String video = containerObject.optString("video");
}
Wenn Sie die optString("key")
-Methode zum Abrufen des String-Werts verwenden, müssen Sie sich keine Sorgen darüber machen, dass Schlüssel in der JsonObject
vorhanden sind oder nicht.
Benutzen
if(containerObject.has("video")){
//get value of video
}
containerObject= new JSONObject(container);
if(containerObject.has("video")){
//get Value of video
}
Aus der Struktur Ihres Quellobjekts würde ich folgendes versuchen:
containerObject= new JSONObject(container);
if(containerObject.has("LabelData")){
JSONObject innerObject = containerObject.getJSONObject("LabelData");
if(innerObject.has("video")){
//Do with video
}
}
Versuchen
private boolean hasKey(JSONObject jsonObject, String key) {
return jsonObject != null && jsonObject.has(key);
}
try {
JSONObject jsonObject = new JSONObject(yourJson);
if (hasKey(jsonObject, "labelData")) {
JSONObject labelDataJson = jsonObject.getJSONObject("LabelData");
if (hasKey(labelDataJson, "video")) {
String video = labelDataJson.getString("video");
}
}
} catch (JSONException e) {
}
Die JSONObject-Klasse hat eine Methode namens "has" . Gibt "true" zurück, wenn für dieses Objekt eine Zuordnung für den Namen vorhanden ist. Die Zuordnung kann NULL sein . http://developer.Android.com/reference/org/json/JSONObject.html#has(Java.lang.String)
Probieren Sie mal das hier an..
JSONObject jsonObject= null;
try {
jsonObject = new JSONObject("result........");
String labelDataString=jsonObject.getString("LabelData");
JSONObject labelDataJson= null;
labelDataJson= new JSONObject(labelDataString);
if(labelDataJson.has("video")&&labelDataJson.getString("video")!=null){
String video=labelDataJson.getString("video");
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONObject root= new JSONObject();
JSONObject container= root.getJSONObject("LabelData");
try{
//if key will not be available put it in the try catch block your program
will work without error
String Video=container.getString("video");
}
catch(JsonException e){
if key will not be there then this block will execute
}
if(video!=null || !video.isEmpty){
//get Value of video
}else{
//other vise leave it
}
ich denke, das könnte dir helfen