You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何从单个JSONObject获取多个JSONArray及其中字符串

嘿,我来帮你搞定Android开发里这个JSON解析的问题!针对你给出的成绩JSON结构,我一步步拆解给你看,还附上完整代码示例,保证你能直接上手用~

整体思路

你的JSON结构是多层嵌套的:最外层是包含marks的对象 → marks里是各个科目的对象 → 每个科目下又有对应考核类型(Theory/ASL/Practical等)的JSONArray。所以我们的解析逻辑就是从外到内逐层拆解,最后遍历数组提取字符串。

详细步骤&代码示例

首先补全你的示例JSON(方便测试)

先把你给出的不完整JSON补全,这样代码能正常运行:

{
  "marks": {
    "Language 1 English": {
      "Theory": ["ASL", "", "", "", ""],
      "ASL": ["ASL", "", "", "", ""]
    },
    "Mathematics": {
      "Theory": ["", "100", "33", "0", "Failed"]
    },
    "Physics": {
      "Theory": ["Practical", "80", "27", "94", "Passed"],
      "Practical": ["Practical", "", "", "", ""]
    },
    "Chemistry": {
      "Theory": ["Practical", "", "", "", ""],
      "Practical": ["Practical", "", "", "", ""]
    },
    "Biology/ Computer Science": {
      "Theory": ["", "90", "30", "0", "Passed"],
      "Practical": ["Practical", "", "", "", ""]
    }
  }
}

Android端解析代码(Java)

下面是完整的解析逻辑,包含异常处理和字符串提取:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MarksJsonParser {

    // 解析成绩JSON的方法
    public static void parseMarksJson(String jsonContent) {
        try {
            // 1. 解析最外层的根对象,拿到marks对应的JSONObject
            JSONObject rootJson = new JSONObject(jsonContent);
            JSONObject marksObject = rootJson.getJSONObject("marks");

            // 2. 遍历所有科目(比如Language 1 English、Mathematics等)
            for (String subjectName : marksObject.keySet()) {
                System.out.println("当前科目:" + subjectName);
                JSONObject subjectObject = marksObject.getJSONObject(subjectName);

                // 3. 遍历该科目下的所有考核类型(Theory/ASL/Practical等)
                for (String assessmentType : subjectObject.keySet()) {
                    System.out.println("  考核类型:" + assessmentType);
                    JSONArray assessmentArray = subjectObject.getJSONArray(assessmentType);

                    // 4. 从JSONArray中逐个提取字符串
                    System.out.print("  内容:");
                    for (int i = 0; i < assessmentArray.length(); i++) {
                        // 用optString更安全,空值或索引越界时返回空字符串,不会崩溃
                        String item = assessmentArray.optString(i);
                        System.out.print("'" + item + "' ");
                    }
                    System.out.println("\n");
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
            // 实际开发中可以在这里处理异常,比如给用户提示"数据解析失败"
        }
    }

    // 测试方法
    public static void main(String[] args) {
        // 把上面的示例JSON字符串传进去测试
        String testJson = "{\n" +
                "  \"marks\": {\n" +
                "    \"Language 1 English\": {\n" +
                "      \"Theory\": [\"ASL\", \"\", \"\", \"\", \"\"],\n" +
                "      \"ASL\": [\"ASL\", \"\", \"\", \"\", \"\"]\n" +
                "    },\n" +
                "    \"Mathematics\": {\n" +
                "      \"Theory\": [\"\", \"100\", \"33\", \"0\", \"Failed\"]\n" +
                "    },\n" +
                "    \"Physics\": {\n" +
                "      \"Theory\": [\"Practical\", \"80\", \"27\", \"94\", \"Passed\"],\n" +
                "      \"Practical\": [\"Practical\", \"\", \"\", \"\", \"\"]\n" +
                "    },\n" +
                "    \"Chemistry\": {\n" +
                "      \"Theory\": [\"Practical\", \"\", \"\", \"\", \"\"],\n" +
                "      \"Practical\": [\"Practical\", \"\", \"\", \"\", \"\"]\n" +
                "    },\n" +
                "    \"Biology/ Computer Science\": {\n" +
                "      \"Theory\": [\"\", \"90\", \"30\", \"0\", \"Passed\"],\n" +
                "      \"Practical\": [\"Practical\", \"\", \"\", \"\", \"\"]\n" +
                "    }\n" +
                "  }\n" +
                "}";

        parseMarksJson(testJson);
    }
}
关键注意点
  • 安全解析优先:用optString()代替getString(),如果数组里有空值或者索引超出范围,optString()会返回空字符串,不会抛出异常导致APP崩溃。
  • 必须处理异常:JSON解析过程中(比如键不存在、格式错误)都会抛出JSONException,一定要用try-catch包裹,避免崩溃。
  • 灵活遍历:用keySet()遍历科目和考核类型,不用硬编码键名,就算后续JSON新增了科目或考核项,代码也能自动适配。

内容的提问来源于stack exchange,提问作者Abdul bari ct

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:41:21