3

In my json response I getting json array and inside it there are 80 other json object and in each object has elements and some of objects has JsonArray also. So how can I check that whether json array is present or not?

I have tried optjsonarray also but it did not work for me.

Here is my code:

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

2 Answers 2

3

Simply check using 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. 
}
Sign up to request clarification or add additional context in comments.

Comments

1

The below code worked for me :-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.