解析包含对象和单个值的嵌套 JSON

Parsing nested JSON containing both Objects and single values

我认为我有所谓的嵌套 JSON,我想使用 Jackson 反序列化为对象。如果程序 class 具有例如 TrackedEntity 和 ProgramStage 类型的对象(参见 JSON 代码),是否也可以自动将子对象解析为 Java 对象?或者是否可以简单地解析各个对象的 "id" 并将它们放入程序对象中的字符串中?

JSON例子如下:

{
programs: 
[
{
    "id": "IpHINAT79UW",
    "created": "2013-03-04T10:41:07.494+0000",
    "trackedEntity": 
    {
        "id": "cyl5vuJ5ETQ",
        "name": "Person"
    },
    "programStages":
    [
        {
            "id": "A03MvHHogjR",
        },
        {
            "id": "ZzYYXq4EJie",
        },
        {
            "id": "AREMvHHogjR",
        },
        {
            "id": "ZzYYXq4fJie",
        }
    ]
},
{
    "id": "IGRINAT79UW",
    "created": "2013-03-04T10:41:07.494+0000",
    "trackedEntity": 
    {
        "id": "cyl5vuJ5ETQ",
        "name": "Person"
    },
    "programStages":
    [
        {
            "id": "A03MvHHogjR",
        },
        {
            "id": "ZzYYXq4fJie",
        },
        {
            "id": "A01MvHHogjR",
        },
        {
            "id": "ZzGYXq4fJie",
        }
    ]
}
]
}

是的。你应该没事的。创建一个代表您的数据的数据结构:

public class Container
{
    public List<ProgramInfo> programs {get;set;}
}

public class ProgramInfo
{
   public string id{get; set;}
   public DateTime created{get;set;}
   public TrackEntity trrack{get;set;}
}

public class TrackEntity
{
    public string id{get;set;}
    public string name{get;set;}
}


//Then call the deserialise or serialize
Container container = new JavaScriptSerializer().Deserialize<Container>(yourString);
public class TrackedEntity
{
    public string id { get; set; }
    public string name { get; set; }
}

public class ProgramStage
{
    public string id { get; set; }
}

public class Program
{
    public string id { get; set; }
    public string created { get; set; }
    public TrackedEntity trackedEntity { get; set; }
    public List<ProgramStage> programStages { get; set; }
}

public class RootObject
{
    public List<Program> programs { get; set; }
}

//Then call the deserialise or serialize
var container = new JavaScriptSerializer().Deserialize<RootObject>(inputjson);

希望对你有用。

一种方法是简单地为各种实体创建 POJO。

如果您为 TrackEntity

假设以下内容
class TrackedEntity {
    private final String id;
    private final String name;

    @JsonCreator
    TrackedEntity(
            @JsonProperty("id") final String id,
            @JsonProperty("name") final String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

那么以下可能适合ProgramStage:

class ProgramStage {
    private final String id;

    @JsonCreator
    ProgramStage(@JsonProperty("id") final String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}

Program class 稍微有点棘手,因为它必须解析某种分区日期。我在此示例中使用了 Java 8 ZonedDateTime 和自定义格式化程序。您还可以将 JSR 310 模块用作 .

class Program {
    private static final DateTimeFormatter FORMATTER = 
            DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSxx");
    private final ZonedDateTime created;
    private final String id;
    private final List<ProgramStage> programStages;
    private final TrackedEntity trackedEntity;

    @JsonCreator
    public static Program of(
            @JsonProperty("id") final String id,
            @JsonProperty("created") final String created,
            @JsonProperty("trackedEntity") final TrackedEntity trackedEntity,
            @JsonProperty("programStages") final List<ProgramStage> programStages) {

        return new Program(
                id, 
                ZonedDateTime.parse(created, FORMATTER), 
                trackedEntity, 
                programStages);
    }

    public Program(
            final String id,
            final ZonedDateTime created,
            final TrackedEntity trackedEntity,
            final List<ProgramStage> programStages) {
        this.id = id;
        this.created = created;
        this.trackedEntity = trackedEntity;
        this.programStages = programStages;
    }

    public ZonedDateTime getCreated() {
        return created;
    }

    public String getId() {
        return id;
    }

    public List<ProgramStage> getProgramStages() {
        return programStages;
    }

    public TrackedEntity getTrackedEntity() {
        return trackedEntity;
    }
}

最后,要修复外部 programs 实体,可以使用以下方法:

class Programs {
    private final List<Program> programs;

    @JsonCreator
    Programs(@JsonProperty("programs") final List<Program> programs) {
        this.programs = programs;
    }

    public List<Program> getPrograms() {
        return programs;
    }
}

要使用整个东西,只需实例化一个 ObjectMapper 并像这样使用 readValue 方法:

final Programs programs = new ObjectMapper().readValue(json, Programs.class);