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

如何在Java中处理嵌套JSON数组并转换为List对象

Alright, let's break down what's happening here and fix this issue step by step.

问题核心

The root problem is clear: your producer is sending a nested double array ([ [ {...}, {...} ] ]) instead of the expected single array ([ {...}, {...} ]) that maps directly to a List<CreateDoc> in Java. That extra outer array is why your deserialization is failing—your consumer is trying to map the outer array to a List<CreateDoc>, but each element in that outer array is another list, not a CreateDoc object.

解决方案

We have two main paths to fix this: fix the producer to send the correct JSON, or adapt the consumer to handle the nested array structure.

This is the cleanest long-term solution—make sure the producer serializes a single List<CreateDoc> instead of wrapping it in another list.

Common Mistake in Producer Code

Chances are, your producer code is accidentally wrapping the List<CreateDoc> in another collection before serializing it. For example, if using Jackson:

// ❌ Wrong: Wraps the list in another singleton list
List<CreateDoc> docList = getCreateDocs();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(Collections.singletonList(docList));

Corrected Producer Code

Serialize the List<CreateDoc> directly:

// ✅ Correct: Serializes the list as a single array
List<CreateDoc> docList = getCreateDocs();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(docList);

If you're using Spring MVC (or Spring Boot) with @ResponseBody or @RestController, just return the List<CreateDoc> directly—Spring's message converters will handle serializing it to a single JSON array automatically:

@GetMapping("/docs")
public List<CreateDoc> getDocs() {
    return getCreateDocs(); // Returns a single JSON array, not nested
}

2. Adapt the Consumer (If Producer Can't Be Changed)

If you can't modify the producer right now, adjust your consumer code to first extract the inner array, then deserialize it to List<CreateDoc>.

Using Jackson

Option 1: Deserialize to a nested list first, then extract the inner list:

ObjectMapper mapper = new ObjectMapper();
// Deserialize the nested structure
TypeReference<List<List<CreateDoc>>> nestedType = new TypeReference<>() {};
List<List<CreateDoc>> nestedDocList = mapper.readValue(json, nestedType);
// Extract the inner list (assuming the outer array has exactly one element)
List<CreateDoc> docList = nestedDocList.get(0);

Option 2: Use JsonNode for more flexibility (useful if you need to validate the structure first):

ObjectMapper mapper = new ObjectMapper();
JsonNode rootNode = mapper.readTree(json);

// Check if the root is an array and has at least one element
if (rootNode.isArray() && rootNode.size() > 0) {
    JsonNode innerArray = rootNode.get(0);
    // Deserialize the inner array to List<CreateDoc>
    List<CreateDoc> docList = mapper.readValue(innerArray.traverse(), new TypeReference<List<CreateDoc>>() {});
}

Using Gson

If you're using Gson instead of Jackson, the approach is similar:

Gson gson = new Gson();
// Define the nested type
Type nestedType = new TypeToken<List<List<CreateDoc>>>(){}.getType();
List<List<CreateDoc>> nestedDocList = gson.fromJson(json, nestedType);
// Extract the inner list
List<CreateDoc> docList = nestedDocList.get(0);
Key Notes
  • Always validate the JSON structure before deserializing (especially if adapting the consumer) to avoid IndexOutOfBoundsException if the outer array is empty.
  • If using Spring on the consumer side, you can create a custom HttpMessageConverter to handle this nested structure automatically, but that's more advanced—stick to the simpler extraction method unless you need to handle this case globally.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:18:38