无法访问JSON

时间:2018-03-14 07:18:11

标签: android json

我无法从我的文件中访问JSON。我的JSON文件内容是:

    {
        "AuditQuestions": [{
            "categoryName": "General",
            "QuestionList": [{
                "quesId": "01",
                "type": "editText",
                "questText": "Inspected By",
                "answer": ""
            },

                {
                    "quesId": "02",
                    "type": "editText",
                    "questText": "This Audit Is For City:",
                    "answer": ""
                },

                {
                    "quesId": "03",
                    "type": "editText",
                    "questText": "This Audit Is For Property:",
                    "answer": ""
                },
    ]
    },

    {
                "categoryName": "Accessibility, Location and Security",

                "QuestionList": [{
                    "quesId": "17",
                    "type": "radio",
                    "questText": "Could you find the property easily with the address given",
                    "answer": ["Yes", "No"]
                },

                    {
                        "quesId": "18",
                        "type": "editText",
                        "questText": "Please suggest what we can add to the address to make it more locatable (eg: what landmark?)",
                        "answer": ""
                    },
    ]
    }
.
.
.
.
.
.
    }]

每个'QuestionList'都有10-15套,但为了简单起见,我跳过了它们。

我访问它的代码是:

public void getQuestion(String page) {

        questionList = new ArrayList<QuestionList>();
        String category;
        String quesId ;
        String questionType ;
        String questionText;
        String answer;

        try {
            JSONObject jsonObj = new JSONObject(page);
            JSONArray jssArray = jsonObj.getJSONArray("AuditQuestions");
            int jsonArrLength = jssArray.length();
            for (int i = 0; i < jsonArrLength; i++) {
                JSONObject jsonChildObj = jssArray.getJSONObject(i);
                category = jsonChildObj.getString("categoryName");
                quesId = jsonChildObj.getJSONObject("QuestionList").getString("quesId");
                questionType = jsonChildObj.getJSONObject("QuestionList").getString("type");
                questionText = jsonChildObj.getJSONObject("QuestionList").getString("questText");
                answer = jsonChildObj.getJSONObject("QuestionList").getString("answer");

                    QuestionList qList = new QuestionList();
                    qList.setQuesId(quesId);
                    qList.setQuesText(questionText);
                    qList.setQuesTypw(questionType);
                    qList.setAnswer(answer);
                    qList.setCategoryName(category);
                    questionList.add(qList);

                }

.
.
.
.
.
}

我试图找出我的代码是否有问题。但它对我来说很好看。 如果我上面使用的代码有问题,请帮忙。 提前谢谢!

3 个答案:

答案 0 :(得分:-1)

试试这个

for (int i = 0; i < jsonArrLength; i++) {
                JSONObject jsonChildObj = jssArray.getJSONObject(i);
                category = jsonChildObj.getString("categoryName");

                JSONArray jsonArrayQueList = jsonChildObj.getJSONArray("QuestionList");

                for(int j = 0 ; j<jsonArrayQueList.length(); j++)
                {
                    JSONObject jsonObjectqueDetail = jsonArrayQueList.getJSONObject(j);

                    quesId = jsonObjectqueDetail.getString("quesId");
                    questionType = jsonObjectqueDetail.getString("type");
                    questionText = jsonObjectqueDetail.getString("questText");
                    answer = jsonObjectqueDetail.getString("answer");

                    QuestionList qList = new QuestionList();
                    qList.setQuesId(quesId);
                    qList.setQuesText(questionText);
                    qList.setQuesTypw(questionType);
                    qList.setAnswer(answer);
                    qList.setCategoryName(category);
                    questionList.add(qList);

                }


            }

答案 1 :(得分:-1)

首先你的json应该关注

{
        "AuditQuestions": 
[
{
            "categoryName": "General",
            "QuestionList": [
{
                "quesId": "01",
                "type": "editText",
                "questText": "Inspected By",
                "answer": ""
            },

                {
                    "quesId": "02",
                    "type": "editText",
                    "questText": "This Audit Is For City:",
                    "answer": ""
                },

                {
                    "quesId": "03",
                    "type": "editText",
                    "questText": "This Audit Is For Property:",
                    "answer": ""
                }]

    },

    {
                "categoryName": "Accessibility, Location and Security",

                "QuestionList": [{
                    "quesId": "17",
                    "type": "radio",
                    "questText": "Could you find the property easily with the address given",
                    "answer": ["Yes", "No"]
                },

                    {
                        "quesId": "18",
                        "type": "editText",
                        "questText": "Please suggest what we can add to the address to make it more locatable (eg: what landmark?)",
                        "answer": ""
                    }]


}]
}

然后像这样解析。我已经解析它并把你的问题问题列表对象

try {
            JSONObject jsonObj = new JSONObject(input);
            JSONArray jssArray = jsonObj.getJSONArray("AuditQuestions");
            int jsonArrLength = jssArray.length();
            for (int i = 0; i < jsonArrLength; i++) {
                JSONObject jsonChildObj = jssArray.getJSONObject(i);
                category = jsonChildObj.getString("categoryName");

                JSONArray jssArray1 = jsonChildObj.getJSONArray("QuestionList");
                for (int i1 = 0; i1 < jssArray1.length(); i1++) {
                    JSONObject jsonChildObj1 = jssArray1.getJSONObject(i1);
                    questionType = jsonChildObj1.getString("type");
                    questionText = jsonChildObj1.getString("questText");
                    answer = jsonChildObj1.getString("answer");
                    quesId = jsonChildObj1.getString("quesId");

                    QuestionList qList = new QuestionList();
                    qList.setQuesId(quesId);
                    qList.setQuesText(questionText);
                    qList.setQuesTypw(questionType);
                    qList.setAnswer(answer);
                    qList.setCategoryName(category);
                    questionList.add(qList);

                }

            }

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

答案 2 :(得分:-1)

首先,您要解析的json数据是错误的。

尝试格式化json字符串,然后解析,

尝试在jsonviewer中添加json数据并进行相应更改。

jsonviewer链接如下:

http://jsonviewer.stack.hu/