【问题标题】:check whether json object contains json array or not?检查json对象是否包含json数组?
【发布时间】:2015-01-24 18:52:07
【问题描述】:

在我的 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
    }
}

【问题讨论】:

标签: android json arrays


【解决方案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();
        }
    }

【讨论】:

    【解决方案2】:

    使用 try-catch case 简单检查:

    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. 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 2020-06-07
      • 2018-04-25
      • 1970-01-01
      • 2019-06-27
      • 2020-01-17
      • 2020-08-09
      相关资源
      最近更新 更多