【问题标题】:Not able to extract array elements from JSON in android无法从android中的JSON中提取数组元素
【发布时间】:2018-03-18 10:17:14
【问题描述】:

我一直在尝试从我的 JSON 中提取数组元素,但目前无法做到。

我有以下 JSON:

{
    "Sample JSON": [{
            "Title": "Title1",
            "Audit": [{
                    "auditId": "01",
                    "type": "sampleType",
                    "auditText": "sampleText",
                    "answer": ["Ans1", "Ans2"]
                },
                {
                    "auditId": "02",
                    "type": "sampleType2",
                    "auditText": "sampleText2",
                    "answer": ["Ans1", "Ans2", "Ans3", "Ans4"]
                }
            ]
        },
        {
            "Title": "Title2",
            "Audit": [{
                    "auditId": "03",
                    "type": "sampleType3",
                    "auditText": "sampleText3",
                    "answer": ["Ans1", "Ans2"]
                },
                {
                    "auditId": "04",
                    "type": "sampleType4",
                    "auditText": "sampleText4",
                    "answer": ["Ans1", "Ans2", "Ans3", "Ans4"]
                }
            ]
        }
    ]
}

我想提取数组'answer'的数组元素。

我在以下方法的注释中指出的行中出现异常:

    public void getAudit(String myJSON) {

        auditList = new ArrayList<AuditList>();
        String title;
        String auditId;
        String type;
        String auditText;
        ArrayList<String> answer = new ArrayList<>();

        try {
            JSONObject jsonObj = new JSONObject(myJSON);
            JSONArray jssArray = jsonObj.getJSONArray("Sample JSON");
            int jsonArrLength = jssArray.length();
            for (int i = 0; i < jsonArrLength; i++) {
                JSONObject jsonChildObj = jssArray.getJSONObject(i);
                title = jsonChildObj.getString("Title");

                JSONArray jssArray1 = jsonChildObj.getJSONArray("Audit");
                for (int i1 = 0; i1 < jssArray1.length(); i1++) {


                    JSONObject jsonChildObj1 = jssArray1.getJSONObject(i1);
                    type = jsonChildObj1.optString("type");
                    auditText = jsonChildObj1.optString("auditText");

                    auditId = jsonChildObj1.optString("auditId");

                    JSONArray jssArray2 = jsonChildObj1.getJSONArray("answer");
                     //Getting exception in above line

                    for (int j=0; j<jssArray2.length(); j++)
                    {
                        answer.add(jssArray2.get(j).toString()) ;
                    }

                    AuditList aList = new AuditList();
                    aList.setQuesId(auditId);
                    aList.setQuesText(auditText);
                    aList.setQuesTypw(auditType);
                    aList.setAnswer(answer);
                    aList.setCategoryName(title);
                    auditList.add(aList);

                }


            }

.
.
.
.
.
}

我使用调试器在上面指出的行中发现了异常。 找不到提取数组“答案”的数组元素的方法。 请帮忙。

【问题讨论】:

  • 哪一行??请分享你的logcat
  • 我在上面的方法中作为注释提到了它: JSONArray jssArray2 = jsonChildObj1.getJSONArray("answer"); //在上面的行中获取异常
  • 异常信息是什么?类型、auditId、auditText 怎么样 - 你是否正确获取了这些值?
  • @lorraine:异常消息是:“org.json.JSONException:java.lang.String 类型的答案值无法转换为 JSONArray”。抱歉回复晚了。

标签: android arrays json


【解决方案1】:
if(!jsonChildObj1.get("answer").toString().equals("")){ 
   JSONArray jssArray2 = jsonChildObj1.getJSONArray("answer");
   for (int j=0; j<jssArray2.length(); j++)
   {
      answer.add(jssArray2.get(j).toString()) ;
   }
}

您因空数据 ("") 而发生的异常。为了防止这种情况,请验证它并 getJSONArray()。 refer

【讨论】:

  • 非常感谢您的帮助!
【解决方案2】:

我做这类事情的方法是转到this link,然后将我的 json 粘贴到那里并获取我的课程。之后,您可以通过将此行添加到您的 build.gradle 来将 Gson 添加到您的 gradle:

compile 'com.google.code.gson:gson:2.6.2'

然后只需创建一个您刚刚创建的已创建类的实例并将 json 转换为您的目标类:

NewClass classObject = new Gson().fromJson(YOUR_JSON, NewClass.class);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多