检查中存在的JSON对象 [英] Check if an object exists in JSON
本文介绍了检查中存在的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要知道如果在一个JSON字符串存在并做取决于物体的存在不同的事情的对象。如果它不存在,我想省略对象,因为它抛出NullPonterException。我已经尝试过使用如果,但没有成功......可有人告诉我,我怎么能检查对象是否存在?
I need to know if an object exists in a JSON string and do different things depending on the existence of that object. If it doesn't exist, I want omit the object because it throws NullPonterException. I've tried using if but no success... Can someone tell me how can I check the existence of an object?
感谢你在前进!
推荐答案
尝试类似如下:
String jsonString = yourJsonString;
String nameOfObjectInQuestion = "yourObjectInQuestion";
JSONObject json = null;
JSONObject objectInQuestion = null;
try {
json = new JSONObject(jsonString);
objectInQuestion = json.getJSONObject(nameOfObjectInQuestion);
}
catch (JSONException ignored) {}
if (objectInQuestion == null) {
// Stomp your feet
}
else {
// Clap your hands
}
这篇关于检查中存在的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文