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

将特定结构JSON字符串转为Java对象数组时遇类型创建错误

Fixing "can't make objects of type SomeObject[]" Error When Parsing JSON to Array

Hey there, let's break down why you're seeing this error and fix it properly!

First, Why the Error Happens

Your original JSON is a top-level JSON object (with keys like obj_1 and obj_2), not a JSON array (which uses [] to wrap items). When you try to parse this directly into a SomeObject[] with Gson, it throws an error because Gson expects an array structure but gets a key-value object instead.

Also, let's note a couple of issues with your SomeObject class upfront:

  • The constructor is misnamed: it should match the class name SomeObject, not Constructor
  • Your JSON has "k1": "v1" (a string value), but SomeObject defines k1 as an int—this will cause another type mismatch error unless you adjust either the JSON or the class (more on that later)

Solution 1: Extract Values from JSONObject to Form an Array

You can first parse the top-level JSON object, extract all its nested object values, convert them to a JSON array, then parse that array into your SomeObject[]:

String jsonString = "{ \"obj_1\": { \"k1\": \"v1\", \"k2\": \"v2\", \"k3\": \"v3\" }, \"obj_2\": { \"k1\": \"v1\", \"k2\": \"v2\", \"k3\": \"v3\" } }";

// Parse the top-level JSON object
JSONObject jsonObject = new JSONObject(jsonString);

// Extract all nested objects into a collection
Collection<?> nestedObjects = jsonObject.values();

// Convert the collection to a JSON array
JSONArray jsonArray = new JSONArray(nestedObjects);

// Use Gson to parse the JSON array into a SomeObject array
Gson gson = new Gson();
SomeObject[] objectsList = gson.fromJson(jsonArray.toString(), SomeObject[].class);

Solution 2: Use Gson to Parse Directly to a Map (No JSONObject Needed)

A cleaner approach is to skip using JSONObject entirely and let Gson parse the JSON into a Map<String, SomeObject>, then convert the map's values to an array:

String jsonString = "{ \"obj_1\": { \"k1\": \"v1\", \"k2\": \"v2\", \"k3\": \"v3\" }, \"obj_2\": { \"k1\": \"v1\", \"k2\": \"v2\", \"k3\": \"v3\" } }";

Gson gson = new Gson();
// Use TypeToken to tell Gson the specific map type we want
Map<String, SomeObject> objMap = gson.fromJson(jsonString, new TypeToken<Map<String, SomeObject>>() {}.getType());

// Convert the map's values to a SomeObject array
SomeObject[] objectsList = objMap.values().toArray(new SomeObject[0]);

Fixing the SomeObject Class

Make sure your class has a correctly named constructor, and address the k1 type mismatch if needed:

public class SomeObject {
    public int k1;
    public String k2;
    public String k3;

    // Corrected constructor name (matches class name)
    public SomeObject(int k1, String k2, String k3) {
        this.k1 = k1;
        this.k2 = k2;
        this.k3 = k3;
    }

    // Optional: Add a no-arg constructor if Gson needs it for deserialization
    public SomeObject() {}
}

If your JSON must keep k1 as a string (like "k1": "v1"), either:

  1. Change k1 in SomeObject to String, or
  2. Add a Gson type adapter to convert the string to an int during parsing

Final Notes

The core issue was trying to parse a JSON object directly into an array type—you need to first transform the object's values into an array structure (either via JSONArray or a map's values) before parsing with Gson.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:08:34