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

如何为指定JSON数据创建Model类?entry数组建模遇阻求助

针对该JSON构建Model的解决方案

第一步:拆解JSON结构

先理清层级关系,核心是entry数组里的每个元素是完整的App条目,需从最内层子结构开始定义,再逐步向外封装:

  1. 通用子结构:多数字段是{ "label": "...", "attributes": {...} }的形式,可拆分出基础标签类和属性类
  2. entry数组元素:包含多个带特殊符号的键名(如im:name),需用对应语言的注解/编码键映射
  3. 外层结构:用Feed类封装entry数组,再用根类封装Feed

第二步:各主流语言实现示例

Swift(基于Codable协议)

通过CodingKeys枚举映射特殊键名:

// 基础标签类(仅含label字段)
struct LabeledModel: Codable {
    let label: String
}

// 价格属性类
struct PriceAttributes: Codable {
    let amount: String
    let currency: String
}

// 价格类
struct PriceModel: Codable {
    let label: String
    let attributes: PriceAttributes
}

// 图片属性类
struct ImageAttributes: Codable {
    let height: String
}

// 图片类
struct ImageModel: Codable {
    let label: String
    let attributes: ImageAttributes
}

// 作者属性类
struct ArtistAttributes: Codable {
    let href: String
}

// 作者类
struct ArtistModel: Codable {
    let label: String
    let attributes: ArtistAttributes
}

// 链接属性类
struct LinkAttributes: Codable {
    let rel: String
    let type: String
    let href: String
}

// 链接类
struct LinkModel: Codable {
    let attributes: LinkAttributes
}

// ID属性类
struct IdAttributes: Codable {
    let imId: String
    let imBundleId: String
    
    enum CodingKeys: String, CodingKey {
        case imId = "im:id"
        case imBundleId = "im:bundleId"
    }
}

// ID类
struct IdModel: Codable {
    let label: String
    let attributes: IdAttributes
}

// 内容类型属性类
struct ContentTypeAttributes: Codable {
    let term: String
    let label: String
}

// 内容类型类
struct ContentTypeModel: Codable {
    let attributes: ContentTypeAttributes
}

// 分类属性类
struct CategoryAttributes: Codable {
    let imId: String
    let term: String
    let scheme: String
    let label: String
    
    enum CodingKeys: String, CodingKey {
        case imId = "im:id"
        case term, scheme, label
    }
}

// 分类类
struct CategoryModel: Codable {
    let attributes: CategoryAttributes
}

// 发布日期属性类
struct ReleaseDateAttributes: Codable {
    let label: String
}

// 发布日期类
struct ReleaseDateModel: Codable {
    let label: String
    let attributes: ReleaseDateAttributes
}

// Entry核心类(对应entry数组的单个元素)
struct EntryModel: Codable {
    let imName: LabeledModel
    let rights: LabeledModel
    let imPrice: PriceModel
    let imImage: [ImageModel]
    let imArtist: ArtistModel
    let title: LabeledModel
    let link: LinkModel
    let id: IdModel
    let imContentType: ContentTypeModel
    let category: CategoryModel
    let imReleaseDate: ReleaseDateModel
    
    enum CodingKeys: String, CodingKey {
        case imName = "im:name"
        case rights
        case imPrice = "im:price"
        case imImage = "im:image"
        case imArtist = "im:artist"
        case title, link, id
        case imContentType = "im:contentType"
        case category
        case imReleaseDate = "im:releaseDate"
    }
}

// Feed类
struct FeedModel: Codable {
    let entry: [EntryModel]
}

// 根结构类
struct RootModel: Codable {
    let feed: FeedModel
}

Kotlin(基于Moshi库)

用@Json注解映射特殊键名:

// 基础标签类
data class LabeledModel(
    val label: String
)

// 价格属性类
data class PriceAttributes(
    val amount: String,
    val currency: String
)

// 价格类
data class PriceModel(
    val label: String,
    val attributes: PriceAttributes
)

// 图片属性类
data class ImageAttributes(
    val height: String
)

// 图片类
data class ImageModel(
    val label: String,
    val attributes: ImageAttributes
)

// 作者属性类
data class ArtistAttributes(
    val href: String
)

// 作者类
data class ArtistModel(
    val label: String,
    val attributes: ArtistAttributes
)

// 链接属性类
data class LinkAttributes(
    val rel: String,
    val type: String,
    val href: String
)

// 链接类
data class LinkModel(
    val attributes: LinkAttributes
)

// ID属性类
data class IdAttributes(
    @Json(name = "im:id") val imId: String,
    @Json(name = "im:bundleId") val imBundleId: String
)

// ID类
data class IdModel(
    val label: String,
    val attributes: IdAttributes
)

// 内容类型属性类
data class ContentTypeAttributes(
    val term: String,
    val label: String
)

// 内容类型类
data class ContentTypeModel(
    val attributes: ContentTypeAttributes
)

// 分类属性类
data class CategoryAttributes(
    @Json(name = "im:id") val imId: String,
    val term: String,
    val scheme: String,
    val label: String
)

// 分类类
data class CategoryModel(
    val attributes: CategoryAttributes
)

// 发布日期属性类
data class ReleaseDateAttributes(
    val label: String
)

// 发布日期类
data class ReleaseDateModel(
    val label: String,
    val attributes: ReleaseDateAttributes
)

// Entry核心类
data class EntryModel(
    @Json(name = "im:name") val imName: LabeledModel,
    val rights: LabeledModel,
    @Json(name = "im:price") val imPrice: PriceModel,
    @Json(name = "im:image") val imImage: List<ImageModel>,
    @Json(name = "im:artist") val imArtist: ArtistModel,
    val title: LabeledModel,
    val link: LinkModel,
    val id: IdModel,
    @Json(name = "im:contentType") val imContentType: ContentTypeModel,
    val category: CategoryModel,
    @Json(name = "im:releaseDate") val imReleaseDate: ReleaseDateModel
)

// Feed类
data class FeedModel(
    val entry: List<EntryModel>
)

// 根结构类
data class RootModel(
    val feed: FeedModel
)

Java(基于Jackson库)

用@JsonProperty注解映射特殊键名:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

// 基础标签类
public class LabeledModel {
    private String label;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
}

// 价格属性类
public class PriceAttributes {
    private String amount;
    private String currency;

    public String getAmount() { return amount; }
    public void setAmount(String amount) { this.amount = amount; }
    public String getCurrency() { return currency; }
    public void setCurrency(String currency) { this.currency = currency; }
}

// 价格类
public class PriceModel {
    private String label;
    private PriceAttributes attributes;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
    public PriceAttributes getAttributes() { return attributes; }
    public void setAttributes(PriceAttributes attributes) { this.attributes = attributes; }
}

// 图片属性类
public class ImageAttributes {
    private String height;

    public String getHeight() { return height; }
    public void setHeight(String height) { this.height = height; }
}

// 图片类
public class ImageModel {
    private String label;
    private ImageAttributes attributes;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
    public ImageAttributes getAttributes() { return attributes; }
    public void setAttributes(ImageAttributes attributes) { this.attributes = attributes; }
}

// 作者属性类
public class ArtistAttributes {
    private String href;

    public String getHref() { return href; }
    public void setHref(String href) { this.href = href; }
}

// 作者类
public class ArtistModel {
    private String label;
    private ArtistAttributes attributes;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
    public ArtistAttributes getAttributes() { return attributes; }
    public void setAttributes(ArtistAttributes attributes) { this.attributes = attributes; }
}

// 链接属性类
public class LinkAttributes {
    private String rel;
    private String type;
    private String href;

    public String getRel() { return rel; }
    public void setRel(String rel) { this.rel = rel; }
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public String getHref() { return href; }
    public void setHref(String href) { this.href = href; }
}

// 链接类
public class LinkModel {
    private LinkAttributes attributes;

    public LinkAttributes getAttributes() { return attributes; }
    public void setAttributes(LinkAttributes attributes) { this.attributes = attributes; }
}

// ID属性类
public class IdAttributes {
    @JsonProperty("im:id")
    private String imId;
    @JsonProperty("im:bundleId")
    private String imBundleId;

    public String getImId() { return imId; }
    public void setImId(String imId) { this.imId = imId; }
    public String getImBundleId() { return imBundleId; }
    public void setImBundleId(String imBundleId) { this.imBundleId = imBundleId; }
}

// ID类
public class IdModel {
    private String label;
    private IdAttributes attributes;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
    public IdAttributes getAttributes() { return attributes; }
    public void setAttributes(IdAttributes attributes) { this.attributes = attributes; }
}

// 内容类型属性类
public class ContentTypeAttributes {
    private String term;
    private String label;

    public String getTerm() { return term; }
    public void setTerm(String term) { this.term = term; }
    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
}

// 内容类型类
public class ContentTypeModel {
    private ContentTypeAttributes attributes;

    public ContentTypeAttributes getAttributes() { return attributes; }
    public void setAttributes(ContentTypeAttributes attributes) { this.attributes = attributes; }
}

// 分类属性类
public class CategoryAttributes {
    @JsonProperty("im:id")
    private String imId;
    private String term;
    private String scheme;
    private String label;

    public String getImId() { return imId; }
    public void setImId(String imId) { this.imId = imId; }
    public String getTerm() { return term; }
    public void setTerm(String term) { this.term = term; }
    public String getScheme() { return scheme; }
    public void setScheme(String scheme) { this.scheme = scheme; }
    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
}

// 分类类
public class CategoryModel {
    private CategoryAttributes attributes;

    public CategoryAttributes getAttributes() { return attributes; }
    public void setAttributes(CategoryAttributes attributes) { this.attributes = attributes; }
}

// 发布日期属性类
public class ReleaseDateAttributes {
    private String label;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
}

// 发布日期类
public class ReleaseDateModel {
    private String label;
    private ReleaseDateAttributes attributes;

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }
    public ReleaseDateAttributes getAttributes() { return attributes; }
    public void setAttributes(ReleaseDateAttributes attributes) { this.attributes = attributes; }
}

// Entry核心类
public class EntryModel {
    @JsonProperty("im:name")
    private LabeledModel imName;
    private LabeledModel rights;
    @JsonProperty("im:price")
    private PriceModel imPrice;
    @JsonProperty("im:image")
    private List<ImageModel> imImage;
    @JsonProperty("im:artist")
    private ArtistModel imArtist;
    private LabeledModel title;
    private LinkModel link;
    private IdModel id;
    @JsonProperty("im:contentType")
    private ContentTypeModel imContentType;
    private CategoryModel category;
    @JsonProperty("im:releaseDate")
    private ReleaseDateModel imReleaseDate;

    // Getters & Setters
    public LabeledModel getImName() { return imName; }
    public void setImName(LabeledModel imName) { this.imName = imName; }
    public LabeledModel getRights() { return rights; }
    public void setRights(LabeledModel rights) { this.rights = rights; }
    public PriceModel getImPrice() { return imPrice; }
    public void setImPrice(PriceModel imPrice) { this.imPrice = imPrice; }
    public List<ImageModel> getImImage() { return imImage; }
    public void setImImage(List<ImageModel> imImage) { this.imImage = imImage; }
    public ArtistModel getImArtist() { return imArtist; }
    public void setImArtist(ArtistModel imArtist) { this.imArtist = imArtist; }
    public LabeledModel getTitle() { return title; }
    public void setTitle(LabeledModel title) { this.title = title; }
    public LinkModel getLink() { return link; }
    public void setLink(LinkModel link) { this.link = link; }
    public IdModel getId() { return id; }
    public void setId(IdModel id) { this.id = id; }
    public ContentTypeModel getImContentType() { return imContentType; }
    public void setImContentType(ContentTypeModel imContentType) { this.imContentType = imContentType; }
    public CategoryModel getCategory() { return category; }
    public void setCategory(CategoryModel category) { this.category = category; }
    public ReleaseDateModel getImReleaseDate() { return imReleaseDate; }
    public void setImReleaseDate(ReleaseDateModel imReleaseDate) { this.imReleaseDate = imReleaseDate; }
}

// Feed类
public class FeedModel {
    private List<EntryModel> entry;

    public List<EntryModel> getEntry() { return entry; }
    public void setEntry(List<EntryModel> entry) { this.entry = entry; }
}

// 根结构类
public class RootModel {
    private FeedModel feed;

    public FeedModel getFeed() { return feed; }
    public void setFeed(FeedModel feed) { this.feed = feed; }
}

核心注意事项

  • 特殊键名处理:im:name这类含特殊符号的键,必须通过语言的注解/编码键映射为合法变量名
  • 数组类型:entry和im:image都是数组,需定义为对应语言的集合类型(如Swift的[T]、Kotlin的List<T>)
  • 嵌套结构:所有嵌套对象都要单独定义Model类,逐层拆解避免混乱

内容的提问来源于stack exchange,提问作者Siddu M S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 05:54:23