【问题标题】:I have json in that there is one other json object so how to check second Json object is null我有 json,因为还有另一个 json 对象,所以如何检查第二个 Json 对象是否为空
【发布时间】:2012-10-09 08:56:29
【问题描述】:
[    
    {
        "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"
    }
]

我有 2 个 JSONArray,它有“节点”对象,有时有值,有时有空值,所以如何检查这个“节点”对象是否有空值

我尝试这种方式。请检查它并告诉我如何解决这个问题 JSONArray

mJSONArray = new JSONArray(response);

for (int i = 0; i < mJSONArray.length(); i++) 
{
  JSONObject mJSONObject = mJSONArray.getJSONObject(i);

  mArrayListName.add(mJSONObject.getString(Constant.VocabularyJsonKey.NAME));
  mArrayListvid.add(mJSONObject.getString(Constant.VocabularyJsonKey.VID));

    if(!mJSONObject.isNull(Constant.VocabularyJsonKey.CONTENT_TYPE))
    {
      JSONObject contentTypeJSONObject = mJSONObject.getJSONObject(Constant.VocabularyJsonKey.CONTENT_TYPE);
      JSONArray contentTypeJSONArray = contentTypeJSONObject.toJSONArray(contentTypeJSONObject.names());

      //for getting content types and set as comma separated string
      for (int j = 0; j < contentTypeJSONArray.length(); j++) 
      {
        if(j ==   contentTypeJSONArray.length()-1) 
           content_types = content_types.concat(contentTypeJSONArray.getString(j).toString());
        else 
          content_types = content_types.concat(contentTypeJSONArray.getString(j).toString() + ", ");
      }
    }
    mArrayListCtype.add(content_types);
}

当我列出比因为获取节点对象空值时发生错误。错误是 org.json.JSONException:在 org.json.JSONObject.getJSONObject( JSONObject.java:573)

【问题讨论】:

  • 你试过了吗? OuterObject.innerObject === null。在你的情况下 object[0].nodes === null
  • JSONObject jsonObject;jsonObject.isNull("name");

标签: java android json


【解决方案1】:
JsonObject!=null

试试这个代码

【讨论】:

    【解决方案2】:

    试试 nodes.length == 0 //空

    【讨论】:

      【解决方案3】:

      nodes 不是 JSONArray 吗?因为您在第二个数据集中有 "nodes": [ ]。

      只需使用它来检查:

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

      如果是 JSONObject 检查:

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

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

      【讨论】:

        【解决方案4】:

        我认为 JSON 数组的类型应该是这样的

        [

        //这是你的第一个对象

        {“类别”:

        {"cat_id":"1","cat_title":"广告与媒体"}},

        //这是你的第二个对象

        {“类别”:

        {"cat_id":"2","cat_title":"汽车与海洋"}}

        ]

        然后您通过以下代码检索它:

         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();
                        }
        

        【讨论】:

          猜你喜欢
          • 2020-11-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-07
          • 2012-06-26
          • 2011-05-16
          • 2015-11-10
          相关资源
          最近更新 更多