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

Android XML解析生成指定结构JSON对象技术咨询

调整XML解析代码生成目标JSON结构

看了你的代码,问题主要出在没有把选项和对应的问题绑定,而且没处理问题本身的文本内容。咱们一步步改,实现你要的JSON结构:

首先明确几个核心要点:

  • 要维护一个当前正在解析的Question对象,每次遇到<question>标签就新建它,里面包含问题文本和对应的选项数组
  • 遇到<choice>时,把选项加到当前Question的选项数组里,而不是全局数组
  • 遇到</question>结束标签时,再把这个Question对象加到总的问题数组中

下面是修改后的完整代码:

// 初始化总的JSON结构:外层是包含question数组的对象
JSONObject result = new JSONObject();
JSONArray questionArray = new JSONArray();

// 用于存储当前正在解析的问题对象
JSONObject currentQuestion = null;
// 用于存储当前问题的选项数组
JSONArray currentChoices = null;

while (eventType != XmlPullParser.END_DOCUMENT) { 
    if(eventType == XmlPullParser.START_DOCUMENT) { 
        Log.e("XML READ","--- Start XML ---"); 
    } else if(eventType == XmlPullParser.START_TAG) { 
        String tagName = xpp.getName();
        if (tagName.equals("question")) { 
            // 遇到question开始标签,初始化当前问题和它的选项数组
            currentQuestion = new JSONObject();
            currentChoices = new JSONArray();
        } else if (tagName.equals("choice")) { 
            // 解析单个选项,键名和目标结构保持一致:ans和iscorrect
            try { 
                JSONObject choice = new JSONObject(); 
                choice.put("ans", xpp.getAttributeValue(null, "value")); 
                choice.put("iscorrect", xpp.getAttributeValue(null, "iscorrect")); 
                currentChoices.put(choice); // 加到当前问题的选项数组里
            } catch (JSONException e) { 
                e.printStackTrace(); 
            } 
        }
    } else if(eventType == XmlPullParser.TEXT) {
        // 处理question标签内的文本内容(也就是问题的q字段值)
        if (currentQuestion != null && !xpp.getText().trim().isEmpty()) {
            try {
                currentQuestion.put("q", xpp.getText().trim());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    } else if(eventType == XmlPullParser.END_TAG) {
        String tagName = xpp.getName();
        if (tagName.equals("question")) { 
            // 遇到question结束标签,把选项数组绑定到当前问题,再加入总数组
            try {
                currentQuestion.put("choices", currentChoices);
                questionArray.put(currentQuestion);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            // 重置当前对象,准备解析下一个问题
            currentQuestion = null;
            currentChoices = null;
        }
    }

    try { 
        eventType = xpp.next(); 
    } catch (XmlPullParserException | IOException e) { 
        e.printStackTrace(); 
    } 
}

// 最后组装外层JSON对象
try {
    result.put("question", questionArray);
    // 这里可以打印或使用最终的result对象
    Log.d("FINAL JSON", result.toString());
} catch (JSONException e) {
    e.printStackTrace();
}

关键修改说明:

  1. 新增currentQuestion和currentChoices:用来跟踪当前解析的问题和它的专属选项,避免不同问题的选项混在一起
  2. 处理TEXT事件:捕获<question>标签内的文本内容,填充到q字段
  3. 处理END_TAG事件:在一个问题解析完成时,把选项数组绑定到该问题,再将问题加入总数组
  4. 最终组装外层结构:把总问题数组放到question键下,完全匹配你要的JSON格式

如果你的XML中question标签还有其他属性,也可以在START_TAG的question分支里用getAttributeValue解析添加。

内容的提问来源于stack exchange,提问作者addykha

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:25:16