如何读取大型JSON文件并通过类映射对象实现数据提取?
Hey there! Mapping that JSON structure to classes is totally straightforward once you mirror the nested hierarchy of your data. Let’s walk through implementations in a few common languages—pick the one that fits your stack!
First, Let’s Break Down Your JSON Structure
Before diving into code, let’s clarify the structure we’re working with:
- Root object with 4 fields:
enabled: Boolean valuecontentMetadataPartial: Nested object with acustomTagskey-value pairsimulatedChanges: Array of integerssimulatedUpdateMetadata: Array of objects (each with varying optional fields likecustomTags,assetName,duration,encodedRate)
Java Implementation (Using Jackson)
Jackson is the go-to library for JSON serialization/deserialization in Java. Here’s how to map your JSON:
First, define the nested classes, then the root class:
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import java.util.List; import java.util.Map; // Nested class for contentMetadataPartial @JsonIgnoreProperties(ignoreUnknown = true) class ContentMetadataPartial { private Map<String, String> customTags; // Getters and setters (or use Lombok for brevity) public Map<String, String> getCustomTags() { return customTags; } public void setCustomTags(Map<String, String> customTags) { this.customTags = customTags; } } // Class for objects in simulatedUpdateMetadata array @JsonIgnoreProperties(ignoreUnknown = true) class SimulatedUpdateMetadata { private Map<String, String> customTags; private String assetName; private Integer duration; private Integer encodedRate; // Getters and setters public Map<String, String> getCustomTags() { return customTags; } public void setCustomTags(Map<String, String> customTags) { this.customTags = customTags; } public String getAssetName() { return assetName; } public void setAssetName(String assetName) { this.assetName = assetName; } public Integer getDuration() { return duration; } public void setDuration(Integer duration) { this.duration = duration; } public Integer getEncodedRate() { return encodedRate; } public void setEncodedRate(Integer encodedRate) { this.encodedRate = encodedRate; } } // Root class that ties everything together @JsonIgnoreProperties(ignoreUnknown = true) class RootJsonObject { private boolean enabled; private ContentMetadataPartial contentMetadataPartial; private List<Integer> simulatedChanges; private List<SimulatedUpdateMetadata> simulatedUpdateMetadata; // Getters and setters public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public ContentMetadataPartial getContentMetadataPartial() { return contentMetadataPartial; } public void setContentMetadataPartial(ContentMetadataPartial contentMetadataPartial) { this.contentMetadataPartial = contentMetadataPartial; } public List<Integer> getSimulatedChanges() { return simulatedChanges; } public void setSimulatedChanges(List<Integer> simulatedChanges) { this.simulatedChanges = simulatedChanges; } public List<SimulatedUpdateMetadata> getSimulatedUpdateMetadata() { return simulatedUpdateMetadata; } public void setSimulatedUpdateMetadata(List<SimulatedUpdateMetadata> simulatedUpdateMetadata) { this.simulatedUpdateMetadata = simulatedUpdateMetadata; } }
Key Notes:
@JsonIgnoreProperties(ignoreUnknown = true)ensures deserialization doesn’t fail if extra fields show up in the JSON.- Use
Map<String, String>forcustomTagssince it’s a dynamic key-value pair. - The
SimulatedUpdateMetadataclass includes all possible fields from the array elements—missing fields will just benull.
C# Implementation (Using System.Text.Json)
For C#, you can use the built-in System.Text.Json library (or Newtonsoft.Json if you prefer). Here’s the class setup:
using System.Collections.Generic; using System.Text.Json.Serialization; public class ContentMetadataPartial { [JsonPropertyName("customTags")] public Dictionary<string, string> CustomTags { get; set; } } public class SimulatedUpdateMetadata { [JsonPropertyName("customTags")] public Dictionary<string, string> CustomTags { get; set; } [JsonPropertyName("assetName")] public string AssetName { get; set; } [JsonPropertyName("duration")] public int? Duration { get; set; } // Nullable int for optional field [JsonPropertyName("encodedRate")] public int? EncodedRate { get; set; } } public class RootJsonObject { [JsonPropertyName("enabled")] public bool Enabled { get; set; } [JsonPropertyName("contentMetadataPartial")] public ContentMetadataPartial ContentMetadataPartial { get; set; } [JsonPropertyName("simulatedChanges")] public List<int> SimulatedChanges { get; set; } [JsonPropertyName("simulatedUpdateMetadata")] public List<SimulatedUpdateMetadata> SimulatedUpdateMetadata { get; set; } }
Key Notes:
- Use
Dictionary<string, string>for dynamiccustomTags. - Mark optional fields (like
DurationandEncodedRate) as nullable (int?) so they can benullwhen missing from the JSON. JsonPropertyNameensures the JSON keys match your C# property names (if you use different naming conventions).
Python Implementation (Using Pydantic)
Pydantic makes this super clean in Python, with automatic validation and serialization. First install Pydantic (pip install pydantic), then define your models:
from pydantic import BaseModel from typing import List, Dict, Optional class ContentMetadataPartial(BaseModel): custom_tags: Dict[str, str] class SimulatedUpdateMetadata(BaseModel): custom_tags: Optional[Dict[str, str]] = None asset_name: Optional[str] = None duration: Optional[int] = None encoded_rate: Optional[int] = None class RootJsonObject(BaseModel): enabled: bool content_metadata_partial: ContentMetadataPartial simulated_changes: List[int] simulated_update_metadata: List[SimulatedUpdateMetadata] # Example deserialization json_str = """{ "enabled":true, "contentMetadataPartial": { "customTags": { "tag1":"value1" } }, "simulatedChanges": [ 3000, 2500, 400 ], "simulatedUpdateMetadata": [ { "customTags": { "tag1":"value1" }, "assetName":"asset1234" }, { "duration":1111, "encodedRate":3333 } ] }""" # Convert JSON to object root_obj = RootJsonObject.model_validate_json(json_str) print(root_obj.enabled) # Output: True
Key Notes:
- Pydantic automatically maps snake_case property names to camelCase JSON keys (thanks to its default settings).
- Use
Optional[...]for fields that might be missing, and set a default ofNone. model_validate_jsonhandles deserialization seamlessly.
Core Takeaway
No matter which language you’re using, the core approach is the same:
- Create a class for each nested object in the JSON.
- Use collection types (like
List,Dictionary) for arrays and key-value pairs. - Handle optional fields properly (nullable types,
Optional, or ignore unknown fields) to avoid deserialization errors.
内容的提问来源于stack exchange,提问作者John Shepard

