检查 JSON 对象是否包含 JSON 数组?

3
在我的json响应中,我得到了一个json数组,里面有80个其他的json对象,在每个对象中都有元素,一些对象也有JsonArray。那么我该如何检查是否存在json数组?
我已经尝试了optjsonarray,但它对我来说没有起作用。
以下是我的代码:
public static void insertMyDiaryValues(final JSONObject jobject)
        throws Throwable {
    String arrOfColumn[] = new String[] { "uuid", "date", "title", "diary",
            "pictureurl", "pictureWidth", "pictureHeight", "child" };
    try {

        SmartTable DiaryData = null;
        DiaryData = SmartApplication.REF_SMART_APPLICATION.getDataHelper()
                .getTableList().get(DBTblNames.MyDiaryValues);
        try {

            JSONArray jarr = new JSONArray(jobject.getJSONArray("diary"));

            for (int i = 0; i < jarr.length(); i++) {
                // String childValue = ""
                // + jarr.getJSONObject(i).getJSONArray("child").length();
                // System.out.println("child Value :-"
                // + jarr.getJSONObject(i).getJSONArray("child").length());

                // if (!(childValue == null)) {

                DiaryData.insertRow(
                        arrOfColumn,
                        new String[] {

                                jarr.getJSONObject(i).get("uuid")
                                        .toString(),
                                jarr.getJSONObject(i).get("date")
                                        .toString(),
                                jarr.getJSONObject(i).get("title")
                                        .toString(),
                                jarr.getJSONObject(i).get("diary")
                                        .toString(),
                                jarr.getJSONObject(i).get("pictureurl")
                                        .toString(),
                                jarr.getJSONObject(i).get("pictureWidth")
                                        .toString(),
                                jarr.getJSONObject(i).get("pictureHeight")
                                        .toString(),
                                ""
                                        + jarr.getJSONObject(i)
                                                .getJSONArray("child")
                                                .length()

                        });

            }

        } catch (Exception e) {

        }

    }

    catch (Exception e) {
        // TODO: handle exception
    }
}

可能是重复的问题:确定JSON是JSONObject还是JSONArray - undefined
不,它不是重复的。 - undefined
2个回答

3

只需使用try-catch语句检查:

try{
     JSONArray jsonArray = jsonObject.getJSONArray("key");
}catch (JSONException e){
     e.printStackTrace();
     // here write your code to get value from Json object which is not getJSONArray. 
}

1
以下代码对我有效:
 public static void insertMyDiaryValues(final JSONObject jobject) {
        String arrOfColumn[] = new String[] { "uuid", "date", "title", "diary",
                "pictureurl", "pictureWidth", "pictureHeight", "child" };

        try {
            SmartTable DiaryData = null;
            DiaryData = SmartApplication.REF_SMART_APPLICATION.getDataHelper()
                    .getTableList().get(DBTblNames.MyDiaryValues);

            JSONArray jarr = new JSONArray(jobject.getJSONArray("diary")
                    .toString());

            for (int i = 0; i < jarr.length(); i++) {

                String child;
                if (jarr.getJSONObject(i).has("child")) {
                    child = ""
                            + jarr.getJSONObject(i).getJSONArray("child")
                                    .length();
                } else {
                    child = "0";
                }
                try {
                    DiaryData.insertRow(arrOfColumn,
                            new String[] {

                                    jarr.getJSONObject(i).get("uuid")
                                            .toString(),
                                    jarr.getJSONObject(i).get("date")
                                            .toString(),
                                    jarr.getJSONObject(i).get("title")
                                            .toString(),
                                    jarr.getJSONObject(i).get("diary")
                                            .toString(),
                                    jarr.getJSONObject(i).get("pictureurl")
                                            .toString(),
                                    jarr.getJSONObject(i).get("pictureWidth")
                                            .toString(),
                                    jarr.getJSONObject(i).get("pictureHeight")
                                            .toString(), child

                            });
                } catch (Throwable e) {

                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (Throwable e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {

        } catch (Exception e) {
            // TODO: handle exception
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接