【发布时间】:2018-07-26 13:11:27
【问题描述】:
如何反序列化在单个类中包含块的 JSON 文件?有没有办法可以使用 Class 中的 JSON 注释来告知属性的父块和属性的父块是什么?
JSON 如下:
{
"Viagem": {
"Id": 33333,
"NumeroAtracacao": "22/2222",
"NumeroViagem": "02002 00303",
"Status": "DESATRACADO",
"Joint": "UCLA UCLA",
"Servico": "AMERICA CENTRAL",
"MotivoEspera": "-",
"LiberacaoRecebimento": "21/05/2018 07:00:00",
"Navio": {
"Nome": "MONTE CERVANTES",
"Armador": {
"Id": 0,
"CodigoGeParcei": null,
"Nome": "ALIANCA",
"Sigla": "ALI",
"CnpjCpf": null,
"Endereco": null,
"Cep": null,
"Site": null
},
"ImagemNavio": ".......",
"Comprimento": 272.08,
"Lloyd": 9283186,
"CallSign": "DHTK",
"CapacidadeTeus": 5560,
"Shortname": "MOCER"
},
"ChegadaPrevista": "27/05/2018 12:00:00",
"AtracacaoPrevista": "29/05/2018 07:00:00",
"SaidaPrevista": "30/05/2018 19:00:00",
"DeadLine": "25/05/2018 12:00:00"
}
}
这是我想用来反序列化 JSON 的类:
namespace WS_SantosBrasil.Model
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
[Table("Viagem")]
public partial class Viagem
{
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int Id { get; set; }
[Key]
[Column(Order = 1)]
[StringLength(50)]
public string IdCtis { get; set; }
[StringLength(20)]
public string NumeroAtracacao { get; set; }
[StringLength(20)]
public string NumeroViagem { get; set; }
[StringLength(50)]
public string Status { get; set; }
[StringLength(50)]
public string Joint { get; set; }
[StringLength(50)]
public string Servico { get; set; }
[StringLength(50)]
public string MotivoEspera { get; set; }
[StringLength(20)]
public string LiberacaoRecebimento { get; set; }
[StringLength(20)]
public string ChegadaPrevista { get; set; }
[StringLength(20)]
public string AtracacaoPrevista { get; set; }
[StringLength(20)]
public string SaidaPrevista { get; set; }
[StringLength(20)]
public string DeadLine { get; set; }
[StringLength(20)]
public string Chegada { get; set; }
[StringLength(20)]
public string Atracacao { get; set; }
[StringLength(20)]
public string Saida { get; set; }
[StringLength(20)]
public string InicioOperacao { get; set; }
[StringLength(20)]
public string FimOperacao { get; set; }
[StringLength(50)]
public string TipoOperacao { get; set; }
[StringLength(20)]
public string CodigoCodesp { get; set; }
public decimal? CaladoAtracacao { get; set; }
public decimal? CaladoDesatracacao { get; set; }
[StringLength(20)]
public string NumeroViagemImportacao { get; set; }
[StringLength(20)]
public string NumeroViagemExportacao { get; set; }
[StringLength(20)]
public string PrevisaoDescarga { get; set; }
[StringLength(20)]
public string PrevisaoEmbarque { get; set; }
[StringLength(20)]
public string PrevisaoRemocao { get; set; }
[StringLength(50)]
public string LocalAtracacao { get; set; }
[StringLength(100)]
public string Navio_Nome { get; set; }
public string ImagemNavio { get; set; }
public decimal? Navio_Comprimento { get; set; }
[StringLength(20)]
public string Navio_Lloyd { get; set; }
[StringLength(50)]
public string Navio_CallSign { get; set; }
public int? Navio_CapacidadeTeus { get; set; }
[StringLength(20)]
public string Navio_Shortname { get; set; }
public int? Armador_Id { get; set; }
[StringLength(20)]
public string Armador_CodigoGeParcei { get; set; }
[StringLength(100)]
public string Armador_Nome { get; set; }
[StringLength(10)]
public string Armador_Sigla { get; set; }
[StringLength(20)]
public string Armador_CnpjCpf { get; set; }
[StringLength(100)]
public string Armador_Endereco { get; set; }
[StringLength(20)]
public string Armador_Cep { get; set; }
[StringLength(100)]
public string Armador_Site { get; set; }
public virtual ContainerViagem ContainerViagem { get; set; }
}
}
在反序列化该类的 JSON 时,我如何告知例如属性 Nome_Navio 位于块 Navio 内并且 JSON 属性为 NOME?
【问题讨论】:
-
把它包在另一个类中,例如:
public class Wrapper { public Viagem Viagem { get; set; }} -
正如其他人所说,在将信息放入 Viagem 类之前,您可能希望将 JSON 字符串反序列化为适当的类。有什么阻止你这样做吗? Visual Studio 可以从您的 JSON 文件中自动生成所需的类(复制 JSON 内容并进行编辑/特殊粘贴/“将 JSON 代码复制为类”)
标签: c# json json.net json-deserialization entity-model