如何检查json数组是否可用并存在? [英] how check is the json Array available and exist?

查看:261
本文介绍了如何检查json数组是否可用并存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中加载一个json数组.但是我需要知道json数组是否存在?因为如果json数组不存在,应用程序将崩溃.

I'm loading a json array in my app.but i need to know if the json array is exist or not? because if the json array isn't exist, the app will crash.

例如,我有这个json:

for example, I have this json:

{ 
"music1":
    [
    {
    "art":"",
                 "artist":"",
    "music":"",
     "flag":"",
                 "text":"",
                 "level":""
 }, 
    {
    "art":"",
                 "artist":"",
    "music":"",
    "flag":"",
                 "text":"",
                 "level":""
 },
]
}

我想知道我的代码中是否存在任何名为"music1"的json对象,然后是否要获取json数组并将其显示在我的Android应用程序的列表中.

and I want to know is there any json object named "music1" in my code or not, and then if there was I want to get the json array and show it in a list in my android app.

我期待一个可以帮助我的英雄!

I'm looking forward a hero that could help me!

推荐答案

我希望此解释能包含在注释中.

I wish this explanation would fit in a comment.

无论如何:

{ 
"music1":
    [
    {
    "art":"",
                 "artist":"",
    "music":"",
     "flag":"",
                 "text":"",
                 "level":""
 }, 
    {
    "art":"",
                 "artist":"",
    "music":"",
    "flag":"",
                 "text":"",
                 "level":""
 }
]
}

调用上面的json对象o. 因此,要检查o是否具有music1,只需检查以下行即可:

Let call the above json object o. So to check if o has music1, all you need to check is write the following line:

if(o.has("music1")){
   JSONArray array= o.getJSONArray("music1");
}

然后您提取json数组的对象:

Then you extract the objects of the json array :

for(int i=0;i<array.length();i++){
     array.getJSONObject(i);
}

这篇关于如何检查json数组是否可用并存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆