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

如何解决org.json.JSONArray无法转换为JSONObject错误并删除指定JSON键值对

问题修复:删除JSON中ProductCharacteristic数组对象的name字段时的类型转换错误

问题场景

需求是删除JSON数据里ProductCharacteristic数组下每个对象的name键值对,但运行代码时抛出org.json.JSONArray cannot be converted to JSONObject错误。

原始JSON内容

[
{
"id": "0028167072_CO_FIX_INTTV_1008_P3909_IDX0",
"status": "Active",
"startDate": "2023-02-12T22:00:00Z",
"place": [
{
"id": "8",
"apartment": "578",
"role": "QA",
"@referredType": "street"
}
],
"ProductCharacteristic": [
{
"id": "CH_100473",
"valueId": "CH12_1000374_VALUE04141",
"value": "LTS",
"name": "Computer"
}
]
}
]

出错的测试代码

public static void main(String[] args) {
  // Replace with your actual JSON string of load JSON from file
  String jsonString = "json above";
  JSONObject jsonObject = new JSONObject(jsonString);
  JSONArray productCharacteristics = jsonObject.getJSONArray("ProductCharacteristic");
  // Iterate through each "ProductCharacteristic" object and remove the "name"
  // key-value pair
  for (int i = 0; i < productCharacteristics.length(); i++) {
    JSONObject productCharacteristic = productCharacteristics.getJSONObject(i);
    productCharacteristic.remove("name");
  }
  String updatedJsonString = jsonObject.toString();
  System.out.println("Modified JSON:\n" + updatedJsonString);
}

错误原因

原始JSON的最外层是JSON数组(用[]包裹),但代码里直接用JSONObject去解析整个字符串,这就触发了类型转换异常——org.json库无法把数组类型的JSON转换成对象类型。

修复后的正确代码

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

public class JsonModifyExample {
    public static void main(String[] args) {
        // 替换为实际的JSON字符串或从文件加载
        String jsonString = "[{\"id\":\"0028167072_CO_FIX_INTTV_1008_P3909_IDX0\",\"status\":\"Active\",\"startDate\":\"2023-02-12T22:00:00Z\",\"place\":[{\"id\":\"8\",\"apartment\":\"578\",\"role\":\"QA\",\"@referredType\":\"street\"}],\"ProductCharacteristic\":[{\"id\":\"CH_100473\",\"valueId\":\"CH12_1000374_VALUE04141\",\"value\":\"LTS\",\"name\":\"Computer\"}]}]";
        
        // 先解析最外层的JSON数组
        JSONArray rootArray = new JSONArray(jsonString);
        
        // 遍历数组中的每个顶层对象
        for (int i = 0; i < rootArray.length(); i++) {
            JSONObject topObject = rootArray.getJSONObject(i);
            // 获取当前对象下的ProductCharacteristic数组
            JSONArray productCharacteristics = topObject.getJSONArray("ProductCharacteristic");
            
            // 遍历每个ProductCharacteristic对象,删除name字段
            for (int j = 0; j < productCharacteristics.length(); j++) {
                JSONObject characteristic = productCharacteristics.getJSONObject(j);
                characteristic.remove("name");
            }
        }
        
        // 输出修改后的JSON(带缩进格式化)
        String updatedJsonString = rootArray.toString(4);
        System.out.println("Modified JSON:\n" + updatedJsonString);
    }
}

关键修改点

  • 把最外层的JSON解析为JSONArray而非JSONObject
  • 增加遍历顶层数组对象的逻辑,再从中获取ProductCharacteristic数组
  • 使用toString(4)输出带缩进的格式化JSON,提升可读性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 19:32:54