org.json.JSONArray无法转换为JSONObject org.json.JSONException:Value

时间:2016-08-27 09:30:09

标签: android arrays json android-studio android-fragments

尝试拉取json数据但不能拉取json数据对象 浪费了1天来解决这个问题,但我得到 org.json.JSONArray无法转换为JSONObject

这是什么错误?任何一个面临相同的问题让我知道阵列数组 下面是我试过的代码。

在谷歌搜索没有得到解决方案

更新 我得到所有数据中的所有数组Array都在相同的输出ExerciseChapters。可以查看“我正在输出”的输出

json输出了我正在做的错误

 if (param_get_ServiceCallResult.equalsIgnoreCase(Constants.GET_USER_MYEXERCISES)) {

                OutputResponseModel outputResponseModel = gson.fromJson(response, OutputResponseModel.class);

                if (outputResponseModel.isSuccess()) {
                    //JSONObject jSONObject1 = new JSONObject(response);
                    //JsonArray jArray = jSONObject1.getJSONArray("data");
                    exerciseChaptersArray = new ArrayList<ExerciseChapters>();

                    try {
                        JSONObject base = new JSONObject(response);
                        JSONArray dataContent = base.getJSONArray("data");
                        for (int i = 0; i < dataContent.length(); i++) {
                            JSONObject eachData = dataContent.getJSONObject(i);

                            testPaperResponseModelObj = new TestPaperResponseModel();
                            testPaperResponseModelObj.setCourseId(eachData.getString("CourseId"));
                            testPaperResponseModelObj.setCourseName(eachData.getString("CourseName"));
                            testPaperResponseModelObj.setCourseIcon(eachData.getString("CourseIcon"));
                            testPaperResponseModelObj.setDownloadedCount(eachData.getString("DownloadedCount"));
                            testPaperResponseModelObj.setRemainingCount(eachData.getString("RemainingCount"));

                            testPaperResponseModelArray.add(testPaperResponseModelObj);

                            String courseId = eachData.getString("CourseId");
                            JSONArray exerciseChapters = eachData.getJSONArray("ExerciseChapters");


                            for (int j = 0; j < exerciseChapters.length(); j++) {
                                JSONObject eachDataexerciseChapters = exerciseChapters.getJSONObject(j);
                                ExerciseChapters exerciseChaptersObj = new ExerciseChapters();
                                exerciseChaptersObj.setCourseId(courseId);
                                exerciseChaptersObj.setCourseChapterId(eachDataexerciseChapters.getString("CourseChapterId"));
                                exerciseChaptersObj.setCourseChapterName(eachDataexerciseChapters.getString("CourseChapterName"));
                                exerciseChaptersObj.setCourseChapterNumber(eachDataexerciseChapters.getString("CourseChapterNumber"));
                                exerciseChaptersObj.setIsFileDownloaded(eachDataexerciseChapters.getString("IsFileDownloaded"));
                                exerciseChaptersArray.add(exerciseChaptersObj);

                            }
                            testPaperResponseModelArray.get(i).setExerciseChapters(exerciseChaptersArray);
                        }



                        Log.i("testPaperRespons***", testPaperResponseModelObj.toString());


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

    }

我正在获得输出

TestPaperResponseModel{CourseId='28', CourseName='Computer Science', CourseIcon='28_fcIcon.jpg', DownloadedCount='0', RemainingCount='0', exerciseChapters=[ExerciseChapters{CourseId='1', CourseChapterId='1', CourseChapterName='Reproduction In Organism', CourseChapterNumber='Chapter-1', IsFileDownloaded='true'}, ExerciseChapters{CourseId='1', CourseChapterId='2', CourseChapterName='Sexual Reproduction In Flowering Plants', CourseChapterNumber='Chapter-2', IsFileDownloaded='true'}, ExerciseChapters{CourseId='1', CourseChapterId='3', CourseChapterName='Human Reproduction', CourseChapterNumber='Chapter-3', IsFileDownloaded='false'}, ExerciseChapters{CourseId='1', CourseChapterId='922', CourseChapterName='Unsolved Guess Paper 1', CourseChapterNumber='Paper- 1', IsFileDownloaded='false'}, ExerciseChapters{CourseId='1', CourseChapterId='923', CourseChapterName='Unsolved Guess Paper - 2', CourseChapterNumber='Paper - 2', IsFileDownloaded='false'}, ExerciseChapters{CourseId='1', CourseChapterId='924', CourseChapterName='Unsolved Guess Paper - 3', CourseChapterNumber='Paper - 3', IsFileDownloaded='false'}, ExerciseChapters{CourseId='3', CourseChapterId='928', CourseChapterName='Unsolved Guess Paper - 1', CourseChapterNumber='Paper - 1', IsFileDownloaded='false'}, ExerciseChapters{CourseId='3', CourseChapterId='929', CourseChapterName='Unsolved Guess Paper - 2', CourseChapterNumber='Paper - 2', IsFileDownloaded='false'}, ExerciseChapters{CourseId='3', CourseChapterId='930', CourseChapterName='Unsolved Guess Paper - 3', CourseChapterNumber='Paper - 3', IsFileDownloaded='false'}, ExerciseChapters{CourseId='6', CourseChapterId='935', CourseChapterName='Unsolved Guess Paper - 1', CourseChapterNumber='Paper - 1', IsFileDownloaded='false'}, ExerciseChapters{CourseId='6', CourseChapterId='936', CourseChapterName='Unsolved Guess Paper - 2', CourseChapterNumber='Paper - 2', IsFileDownloaded='false'}, ExerciseChapters{CourseId='6', CourseChapterId='937', CourseChapterName='Unsolved Guess Paper - 3', CourseChapterNumber='Paper - 3', IsFileDownloaded='false'}, ExerciseChapters{CourseId='10', CourseChapterId='944', CourseChapterName='Unsolved Guess Paper - 1', CourseChapterNumber='Paper - 1', IsFileDownloaded='false'}, ExerciseChapters{CourseId='10', CourseChapterId='945', CourseChapterName='Unsolved Guess Paper - 2', CourseChapterNumber='Paper - 2', IsFileDownloaded='false'}, ExerciseChapters{CourseId='10', CourseChapterId='946', CourseChapterName='Unsolved Guess Paper - 3', CourseChapterNumber='Paper - 3', IsFileDownloaded='false'}, ExerciseChapters{CourseId='11', CourseChapterId='947', CourseChapterName='Unsolved Guess Paper - 1', CourseChapterNumber='Paper - 1', IsFileDownloaded='false'}, ExerciseChapters{CourseId='11', CourseChapterId='948', CourseChapterName='Unsolved Guess Paper - 2', CourseChapterNumber='Paper - 2', IsFileDownloaded='false'}, ExerciseChapters{CourseId='11', CourseChapterId='949', CourseChapterName='Unsolved Guess Paper - 3', CourseChapterNumber='Paper - 3', IsFileDownloaded='false'}, ExerciseChapters{CourseId='12', CourseChapterId='990', CourseChapterName='Unsolved Guess Paper - 1', CourseChapterNumber='Paper - 1', IsFileDownloaded='false'}, ExerciseChapters{CourseId='12', CourseChapterId='991', CourseChapterName='Unsolved Guess Paper - 2', CourseChapterNumber='Paper - 2', IsFileDownloaded='false'}, ExerciseChapters{CourseId='12', CourseChapterId='992', CourseChapterName='Unsolved Guess Paper - 3', CourseChapterNumber='Paper - 3', IsFileDownloaded='false'}]}

Json输出

{
       "isSuccess": true,
      "message": "",
      "data": [
        {
          "CourseId": 1,
          "CourseName": "Biology",
          "CourseIcon": "1_fcIcon.jpg",
          "DownloadedCount": 2,
          "RemainingCount": 4,
          "ExerciseChapters": [
            {
              "CourseChapterId": 1,
              "CourseChapterName": "Reproduction In Organism",
              "CourseChapterNumber": "Chapter-1",
              "IsFileDownloaded": true
            },
            {
              "CourseChapterId": 2,
              "CourseChapterName": "Sexual Reproduction In Flowering Plants",
              "CourseChapterNumber": "Chapter-2",
              "IsFileDownloaded": true
            },
            {
              "CourseChapterId": 3,
              "CourseChapterName": "Human Reproduction",
              "CourseChapterNumber": "Chapter-3",
              "IsFileDownloaded": false
            },
            {
              "CourseChapterId": 922,
              "CourseChapterName": "Unsolved Guess Paper 1",
              "CourseChapterNumber": "Paper- 1",
              "IsFileDownloaded": false
            },
            {
              "CourseChapterId": 923,
              "CourseChapterName": "Unsolved Guess Paper - 2",
              "CourseChapterNumber": "Paper - 2",
              "IsFileDownloaded": false
            },
            {
              "CourseChapterId": 924,
              "CourseChapterName": "Unsolved Guess Paper - 3",
              "CourseChapterNumber": "Paper - 3",
              "IsFileDownloaded": false
            }
          ]
        },
        {
          "CourseId": 3,
          "CourseName": "Chemistry",
          "CourseIcon": "3_fcIcon.jpg",
          "DownloadedCount": 0,
          "RemainingCount": 3,
          "ExerciseChapters": [
            {
              "CourseChapterId": 928,
              "CourseChapterName": "Unsolved Guess Paper - 1",
              "CourseChapterNumber": "Paper - 1",
              "IsFileDownloaded": false
            },
            {
              "CourseChapterId": 929,
              "CourseChapterName": "Unsolved Guess Paper - 2",
              "CourseChapterNumber": "Paper - 2",
              "IsFileDownloaded": false
            },
            {
              "CourseChapterId": 930,
              "CourseChapterName": "Unsolved Guess Paper - 3",
              "CourseChapterNumber": "Paper - 3",
              "IsFileDownloaded": false
            }
          ]
        },
        {
          "CourseId": 28,
          "CourseName": "Computer Science",
          "CourseIcon": "28_fcIcon.jpg",
          "DownloadedCount": 0,
          "RemainingCount": 0,
          "ExerciseChapters": []
        }
      ]
     }

TestPaperResponseModel .java

公共类TestPaperResponseModel实现Serializable {

private String CourseId;
private String CourseName;
private String CourseIcon;
private String DownloadedCount;
private String RemainingCount;
private List<ExerciseChapters> exerciseChapters;

}

2 个答案:

答案 0 :(得分:1)

if (outputResponseModel.isSuccess()) {
                JSONObject jSONObject1 = new JSONObject(response);
                JsonArray jArray = jSONObject1.getJSONArray("data");

                try {
                    for (JsonElement obj : jArray) {
                        testPaperResponseModelObj = gson.fromJson(obj, TestPaperResponseModel.class);
                        testPaperResponseModelArray.add(testPaperResponseModelObj);

                        JSONObject jsssonDashboardChildContent = new JSONObject(dataContent);
                        JsonArray Jarray = jsssonDashboardChildContent.getJSONArray("ExerciseChapters");

                        exerciseChaptersArray = new ArrayList<ExerciseChapters>();
                        for (JsonElement obj1 : Jarray) {
                            ExerciseChapters exerciseChaptersobj = gson.fromJson(obj1, ExerciseChapters.class);
                            exerciseChaptersArray.add(exerciseChaptersobj);
                        }



                        if (testPaperResponseModelObj != null) {
                            testPaperResponseModelObj.setExerciseChapters(exerciseChaptersArray);
                        }
                    }
                } catch (JsonSyntaxException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

}

答案 1 :(得分:1)

尝试像这样获取数组

Dim objCon, objRs
set objCon = createobject("ADODB.Connection")
objCon.Open "Provider = Microsoft.ACE.OLEDB.12.0;" & "Data Source = O:\Desktop\UFT\Testcases.xlsx;" & "Extended Properties = ""Excel 12.0 Xml;HDR = YES;"";" 

Set objRs = createobject("ADODB.Recordset")

objRs.Open "select count(*) from Sheet1 where status = 'Fail'", objcon

msgbox objRs(0)

Set objCon = nothing

Set objRs = nothing