Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我有json,因为还有一个json对象,所以如何检查第二个json对象是否为空_Java_Android_Json - Fatal编程技术网

Java 我有json,因为还有一个json对象,所以如何检查第二个json对象是否为空

Java 我有json,因为还有一个json对象,所以如何检查第二个json对象是否为空,java,android,json,Java,Android,Json,我有两个JSONArray,它有“node”对象,有时有值,有时有空值,所以如何检查这个“node”对象有空值 我试着这样做。请检查一下,告诉我如何解决这个问题 [ { "vid": "45", "name": "hello", "description": "hello", "help": "hello", "relations": "1", "hierarchy": "1",

我有两个JSONArray,它有“node”对象,有时有值,有时有空值,所以如何检查这个“node”对象有空值

我试着这样做。请检查一下,告诉我如何解决这个问题

[    
    {
        "vid": "45",
        "name": "hello",
        "description": "hello",
        "help": "hello",
        "relations": "1",
        "hierarchy": "1",
        "multiple": "1",
        "required": "0",
        "tags": "1",
        "module": "taxonomy",
        "weight": "7",
        "nodes": {
            "hidrupal": "hidrupal",
            "page": "page",
            "recipee": "recipee",
            "story": "story"
        },
        "uri": "http://andrd.yarrait.com/drupal6/android/vocabulary/45"
    },
    {
        "vid": "48",
        "name": "sample",
        "description": "asldfawi iawbfuiasguda jasbf jasfujad jashbdfajsdgawidba kjascbi9wa jhasbjabcuasv askjh bw ajs bjsbjda jsawioabfscja awjdbfuwga ajcsjfgjaw ajdfajs fj agfjweu8 ajsfbjw u",
        "help": "ajfawyg ahbcoiw ygajc jawhbu bua jagbwjwcwug ua jwsajwiuhcg ha bwujagua jajdwgugd uma jau wyg j h jja hg ug auj gwj",
        "relations": "1",
        "hierarchy": "1",
        "multiple": "1",
        "required": "0",
        "tags": "1",
        "module": "taxonomy",
        "weight": "7",
        "type": null,
        "nodes": [ ],
        "uri": "http://andrd.yarrait.com/drupal6/android/vocabulary/48"
    }
]
mJSONArray=新的JSONArray(响应);
对于(int i=0;i
由于获取节点对象空值,我列出时出错。错误是 org.json.JSONException:org.json.JSONArray类型节点上的值[]无法转换为org.json.json.typemisch(json.java:96)org.json.JSONObject.getJSONObject(JSONObject.java:573)上的JSONObject

试试这段代码
nodes.length==0//empty

不是节点JSONArray??因为您在第二个数据集中有“节点”:[]

只需使用此项检查:

JsonObject!=null
如果是JSONObject检查:

parentJSON.getJSONArray("nodes").length() == 0;

JSONObject上的方法length()给出了JSONObject中NameValuePairs的数量。

我认为JSON数组的类型应该与此类似

[

//这是你的第一个目标

{“类别”:

{“类别id”:“1”,“类别标题”:“广告与媒体”}

//这是你的第二个目标

{“类别”:

{“cat_id”:“2”,“cat_标题”:“汽车和船舶”}

]

您可以通过以下代码检索它:

parentJSON.getJSONObject("nodes").length() == 0;
for(int i=0;i
您试过了吗
OuterObject.innerObject==null
。在本例中,对象[0]。节点===nullJSONObject jsonObject;jsonObject.isNull(“名称”);
parentJSON.getJSONObject("nodes").length() == 0;
 for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String outerstr  = jsonObject.getString("category");

                    JSONObject json = new JSONObject(outerstr);

                    String id = json.getString("cat_id");
                    String title = json.getString("cat_title");

                    Log.i("title  ", title);                
                    Log.i("desc  ", id);

                    yourList.add( new yourClassObject(id,title));
                  }

                } catch (Exception e) {
                  e.printStackTrace();
                }