尝试从JSONObject中提取JSONObject时出现“无值”

时间:2013-03-07 16:22:49

标签: java android json

问题似乎很简单,但行为真的很奇怪。

JSON我正在解析:

[
  {
    "ClassId": 1,
    "Description": "Agile Development 202",
    "Schedule": {
      "$id": "27",
      "DateScheduled": "2013-03-06T00:00:00",
      "ScheduleId": 1
    },
    "Questions": [
      {
        "Choices": [
          {
            "QuestionId": 1,
            "IsCorrect": true,
            "ChoiceId": 1,
            "Points": 12,
            "$id": "3",
            "Choice1": "Peer pressure, Fine-grain coordination, Focusing on the few, Daily commitment and Raising impediments."
          },
          {
            "QuestionId": 1,
            "IsCorrect": false,
            "ChoiceId": 2,
            "Points": 0,
            "$id": "4",
            "Choice1": "Know the work, Get a fresh start, Commit to shared goals and Create focus and abundance."
          },
          {
            "QuestionId": 1,
            "IsCorrect": false,
            "ChoiceId": 3,
            "Points": 0,
            "$id": "5",
            "Choice1": "True-up, Show and tell, Get direct feedback, Offer insights and Ask for help."
          },
          {
            "QuestionId": 1,
            "IsCorrect": false,
            "ChoiceId": 5,
            "Points": 0,
            "$id": "6",
            "Choice1": "Inspect and adapt. Look back at how, not what\r\nDo (even) better next time."
          }
        ],
        "QuestionId": 1,
        "TimeAverage": "00:10:00",
        "ExamId": 1,
        "Question1": "You are a coach facilitating retrospective meeting. What is the main purpose of this meeting that facilitator need to ensure during the meeting?",
        "$id": "2"
      },
      {
        "Choices": [
          {
            "QuestionId": 2,
            "IsCorrect": false,
            "ChoiceId": 6,
            "Points": 0,
            "$id": "8",
            "Choice1": "Automated Acceptance Tests"
          },
          {
            "QuestionId": 2,
            "IsCorrect": true,
            "ChoiceId": 7,
            "Points": 15,
            "$id": "9",
            "Choice1": "Continuous Integration"
          },
          {
            "QuestionId": 2,
            "IsCorrect": false,
            "ChoiceId": 8,
            "Points": 0,
            "$id": "10",
            "Choice1": "Unit Test"
          },
          {
            "QuestionId": 2,
            "IsCorrect": false,
            "ChoiceId": 9,
            "Points": 0,
            "$id": "11",
            "Choice1": "Code coverage metrics"
          }
        ],
        "QuestionId": 2,
        "TimeAverage": "00:10:00",
        "ExamId": 1,
        "Question1": "How does tools like FIT and Fitness compliment Agile?",
        "$id": "7"
      },
      {
        "Choices": [
          {
            "QuestionId": 3,
            "IsCorrect": true,
            "ChoiceId": 10,
            "Points": 13,
            "$id": "13",
            "Choice1": "Fast"
          },
          {
            "QuestionId": 3,
            "IsCorrect": false,
            "ChoiceId": 11,
            "Points": 0,
            "$id": "14",
            "Choice1": "Flexible"
          },
          {
            "QuestionId": 3,
            "IsCorrect": false,
            "ChoiceId": 12,
            "Points": 0,
            "$id": "15",
            "Choice1": "Frequent"
          },
          {
            "QuestionId": 3,
            "IsCorrect": false,
            "ChoiceId": 13,
            "Points": 0,
            "$id": "16",
            "Choice1": "Fixed"
          }
        ],
        "QuestionId": 3,
        "TimeAverage": "00:05:00",
        "ExamId": 1,
        "Question1": "What does \"Timeboxed\" means in Agile terms?",
        "$id": "12"
      },
      {
        "Choices": [
          {
            "QuestionId": 4,
            "IsCorrect": true,
            "ChoiceId": 14,
            "Points": 22,
            "$id": "18",
            "Choice1": "They are used to prioritize projects."
          },
          {
            "QuestionId": 4,
            "IsCorrect": false,
            "ChoiceId": 15,
            "Points": 0,
            "$id": "19",
            "Choice1": "They are used to monitor project performance"
          },
          {
            "QuestionId": 4,
            "IsCorrect": false,
            "ChoiceId": 16,
            "Points": 0,
            "$id": "20",
            "Choice1": "They are used to analyze risk."
          },
          {
            "QuestionId": 4,
            "IsCorrect": false,
            "ChoiceId": 17,
            "Points": 0,
            "$id": "21",
            "Choice1": "They are used to measure coach's performance."
          }
        ],
        "QuestionId": 4,
        "TimeAverage": "00:15:00",
        "ExamId": 1,
        "Question1": "How are EVM metrics used in Agile?",
        "$id": "17"
      },
      {
        "Choices": [
          {
            "QuestionId": 5,
            "IsCorrect": true,
            "ChoiceId": 19,
            "Points": 11,
            "$id": "23",
            "Choice1": "10 Iterations"
          },
          {
            "QuestionId": 5,
            "IsCorrect": false,
            "ChoiceId": 20,
            "Points": 0,
            "$id": "24",
            "Choice1": "10 waves"
          },
          {
            "QuestionId": 5,
            "IsCorrect": false,
            "ChoiceId": 21,
            "Points": 0,
            "$id": "25",
            "Choice1": "20 Iterations"
          },
          {
            "QuestionId": 5,
            "IsCorrect": false,
            "ChoiceId": 23,
            "Points": 0,
            "$id": "26",
            "Choice1": "20 waves"
          }
        ],
        "QuestionId": 5,
        "TimeAverage": "00:12:00",
        "ExamId": 1,
        "Question1": "If a team can complete 10 story points in an iteration then how long will it take for the team to complete 100 story points?",
        "$id": "22"
      }
    ],
    "Details": "What is Agile software Development? I explain agile with one dozen words: Iterative planning, honest plans, project heartbeat, working software, team ...",
    "ExamId": 1,
    "$id": "1",
    "Title": "Agile Part Two",
    "TotalTime": "03:00:00"
  }
]

在我尝试加密Schedule JSONObject之前,一切似乎都很好。

一些代码。

public static Exam getExamFromJSONObject(JSONObject jsonObj) throws JSONException {
        int examId = jsonObj.getInt(ExamJSONTags.TAG_ID);
        String title = jsonObj.getString(ExamJSONTags.TAG_TITLE);
        int classId = jsonObj.getInt(ExamJSONTags.TAG_CLASSID);
        String description = jsonObj.getString(ExamJSONTags.TAG_DESCRIPTION);
        String details = jsonObj.getString(ExamJSONTags.TAG_DETAILS);
        Time totalTime = Time.valueOf(jsonObj.getString(ExamJSONTags.TAG_TOTALTIME));

        Exam exam = new Exam();
        exam.setExamId(examId);
        exam.setTitle(title);
        exam.setClassId(classId);
        exam.setDescription(description);
        exam.setDetails(details);
        exam.setTotalTime(totalTime);

        JSONArray questionsJSONArray = jsonObj.getJSONArray(ExamJSONTags.TAG_QUESTIONS);
        ArrayList<Question> questionsArrayList = new ArrayList<Question>();
        if (questionsJSONArray != null) {
            for (int i = 0; i < questionsJSONArray.length(); i++) {
                JSONObject questionJSON = questionsJSONArray.getJSONObject(i);
                Question questionModel = Question.getQuestionFromJSONObject(questionJSON);
                questionModel.setExam(exam);
                questionsArrayList.add(questionModel);
                JSONArray choices = questionJSON.getJSONArray(QuestionJSONTags.TAG_CHOICES);
                if (choices != null) {
                    ArrayList<Choice> choicesArrayList = new ArrayList<Choice>();
                    questionModel.setChoices(choicesArrayList);
                    for (int x = 0; x < choices.length(); x++) {
                        JSONObject choiceJSON = choices.getJSONObject(x);
                        Choice choiceModel = Choice.getChoiceFromJSONObject(choiceJSON);
                        choiceModel.setQuestion(questionModel);
                        choicesArrayList.add(choiceModel);
                    }
                }
            }
        }

        exam.setQuestions(questionsArrayList);

        JSONObject scheduleJSON = jsonObj.getJSONObject(ExamJSONTags.TAG_SCHEDULE); // This is where I get the exception, though you can see the object exists perfectly fine!!!
        if (scheduleJSON != null) {
            Schedule schedule = Schedule.getScheduleFromJSONObject(scheduleJSON);
            exam.setSchedule(schedule);
            schedule.setExam(exam);
        }

        return exam;
    }

谢谢。

1 个答案:

答案 0 :(得分:0)

也许常量值与json属性不匹配,检查区分大小写