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

如何读取大型JSON文件并通过类映射对象实现数据提取?

Mapping Your JSON Structure to Classes: Practical Solutions

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 value
    • contentMetadataPartial: Nested object with a customTags key-value pair
    • simulatedChanges: Array of integers
    • simulatedUpdateMetadata: Array of objects (each with varying optional fields like customTags, 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> for customTags since it’s a dynamic key-value pair.
  • The SimulatedUpdateMetadata class includes all possible fields from the array elements—missing fields will just be null.

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 dynamic customTags.
  • Mark optional fields (like Duration and EncodedRate) as nullable (int?) so they can be null when missing from the JSON.
  • JsonPropertyName ensures 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 of None.
  • model_validate_json handles deserialization seamlessly.

Core Takeaway

No matter which language you’re using, the core approach is the same:

  1. Create a class for each nested object in the JSON.
  2. Use collection types (like List, Dictionary) for arrays and key-value pairs.
  3. Handle optional fields properly (nullable types, Optional, or ignore unknown fields) to avoid deserialization errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:08:16