我正在尝试像这样反序列化一个json。
{
"geneslist": [
{
"seqid": "NC_045512.2",
"source": "RefSeq",
"type": "region",
"start": "1",
"end": "29903",
"score": ".",
"strand": "+",
"phase": ".",
"attributes": "ID=NC_045512.2:1..29903;Dbxref=taxon:2697049;collectiondate=Dec-2019;country=China;gbacronym=SARS-CoV2;gbkey=Src;genome=genomic;isolate=Wuhan-Hu-1;mol_type=genomic RNA;nathost=Homo sapiens;oldname=Wuhan seafood market pneumonia virus",
"ID": "NC_045512.2:1..29903",
"Note": null,
"Dbxref": "taxon:2697049",
"collectiondate": "Dec-2019",
"country": "China",
"gbacronym": "SARS-CoV2",
"gbkey": "Src",
"gene": null,
"inference": null,
"genome": "genomic",
"isolate": "Wuhan-Hu-1",
"locus_tag": null,
"gene_biotype": null,
"product": null,
"protein_id": null,
"mol_type": "genomic RNA",
"nathost": "Homo sapiens",
"oldname": "Wuhan seafood market pneumonia virus"
},
{
"seqid": "NC_045512.2",
"source": "RefSeq",
"type": "five_prime_UTR",
"start": "1",
"end": "265",
"score": ".",
"strand": "+",
"phase": ".",
"attributes": "ID=id-NC_045512.2:1..265;gbkey=5'UTR",
"ID": "id-NC_045512.2:1..265",
"Note": null,
"function": null,
"Dbxref": null,
"collectiondate": null,
"country;": null,
"gbkey": "5'UTR",
"Dbxrref": null,
"country": null,
"gbacronym": null,
"gene": null,
"inference": null,
"genome": null,
"isolate": null,
"locus_tag": null,
"gene_biotype": null,
"product": null,
"protein_id": null,
"mol_type": null,
"nathost": null,
"oldname": null
},{..} ]
}在某些数据可能为空或存在的情况下,我为此创建了两个对象:
public class GenesList
{
private string seqid;
private string source;
private string type;
private int start;
private int end;
private string score;
private string strand;
private string phase;
private string attributes;
private string ID;
private string Note;
private string function;
private string Dbxref;
private string collectiondate;
private string country;
private string gbacronym;
private string gbkey;
private string gene;
private string inference;
private string genome;
private string isolate;
private string locus_tag;
private string gene_biotype;
private string product;
private string protein_id;
private string mol_type;
private string nathost;
private string oldname;
public string Seqid { get => seqid; set => seqid = value; }
public string Source {get => source; set => source = value; }
public string Type { get => type; set => type = value; }
public int Start { get => start; set => start = value; }
public int End { get => end; set => end = value; }
public string Score { get => score; set => score = value; }
public string Strand { get => strand; set => strand = value; }
public string Phase { get => phase; set => phase = value; }
public string Attributes { get => attributes; set => attributes = value; }
public string ID1 { get => ID; set => ID = value; }
public string Dbxref1 { get => Dbxref; set => Dbxref = value; }
public string Collectiondate { get => collectiondate; set => collectiondate = value; }
public string Country { get => country; set => country = value; }
public string Gbacronym { get => gbacronym; set => gbacronym = value; }
public string Gbkey { get => gbkey; set => gbkey = value; }
public string Genome { get => genome; set => genome = value; }
public string Isolate { get => isolate; set => isolate = value; }
public string Mol_type { get => mol_type; set => mol_type = value; }
public string Nathost { get => nathost; set => nathost = value; }
public string Oldname { get => oldname; set => oldname = value; }
public string Locus_tag { get => locus_tag; set => locus_tag = value; }
public string Gene_biotype { get => gene_biotype; set => gene_biotype = value; }
public string Function { get => function; set => function = value; }
public string Inference { get => inference; set => inference = value; }
public string Product { get => product; set => product = value; }
public string Protein_id { get => protein_id; set => protein_id = value; }
public string GetGene()
{
return gene;
}
public void SetGene(string value)
{
gene = value;
}
}和
public class GenesWrapper
{
private List<GenesList> geneslist;
public List<GenesList> Genes { get => geneslist; set => geneslist = value; }
}但是当我尝试反序列化
GenesWrapper genesList = new GenesWrapper();
genesList = JsonConvert.DeserializeObject<GenesWrapper>(file2);我得到了一个ArgomentNullException,因为对象在反序列化后是空的,我是不是构建了一个错误的对象?起初,我认为它可能存在一些空值问题,或者可能是json文件的顺序问题。我使用了NullValueHandling.Ignore,但是值仍然是空的,我该如何解决?很抱歉给你这么多文字,谢谢
发布于 2020-05-30 07:02:32
这是不可行的。您的json不适合创建C#类。属性"country“在第一个(第二个) json对象中出现了两次。
"country;": null,
"gbkey": "5'UTR",
"Dbxrref": null,
"country": null,此外,visual studio可以为您创建代码以防止人为错误。只需像这样创建C#代码,就可以了。

发布于 2020-05-30 07:27:50
您可以这样做:
public class GenesList
{
[JsonProperty("seqid")]
public string Seqid { get; set; }
[JsonProperty("source")]
public string Source { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("start")]
[JsonConverter(typeof(ParseStringConverter))]
public int Start { get; set; }
[JsonProperty("end")]
[JsonConverter(typeof(ParseStringConverter))]
public int End { get; set; }
[JsonProperty("score")]
public string Score { get; set; }
[JsonProperty("strand")]
public string Strand { get; set; }
[JsonProperty("phase")]
public string Phase { get; set; }
[JsonProperty("attributes")]
public string Attributes { get; set; }
[JsonProperty("ID")]
public string Id { get; set; }
[JsonProperty("Note")]
public string Note { get; set; }
[JsonProperty("Dbxref")]
public string Dbxref { get; set; }
[JsonProperty("collectiondate")]
public string Collectiondate { get; set; }
[JsonProperty("country")]
public string Country { get; set; }
[JsonProperty("gbacronym")]
public string Gbacronym { get; set; }
[JsonProperty("gbkey")]
public string Gbkey { get; set; }
[JsonProperty("gene")]
public string Gene { get; set; }
[JsonProperty("inference")]
public string Inference { get; set; }
[JsonProperty("genome")]
public string Genome { get; set; }
[JsonProperty("isolate")]
public string Isolate { get; set; }
[JsonProperty("locus_tag")]
public string LocusTag { get; set; }
[JsonProperty("gene_biotype")]
public string GeneBiotype { get; set; }
[JsonProperty("product")]
public string Product { get; set; }
[JsonProperty("protein_id")]
public string ProteinId { get; set; }
[JsonProperty("mol_type")]
public string MolType { get; set; }
[JsonProperty("nathost")]
public string Nathost { get; set; }
[JsonProperty("oldname")]
public string Oldname { get; set; }
}然后使用JObject导航到geneslist,然后获取字符串以对其进行反序列化。如下所示:
var jObj = JObject.Parse(json);
var genes = jObj["geneslist"].ToString();
var genesList = JsonConvert.DeserializeObject<List<GenesList>>(genes);如果您只需要对geneslist进行反序列化,而忽略json的其余部分。如果您想反序列化整个json键,则需要一个包装类来定义json根目录中的每个属性。如下所示:
public class JsonWrapper
{
[JsonProperty("geneslist")]
public IEnumerable<GenesList> Genes { get; set; }
// ... other json keys..
}然后,您可以直接执行此操作:
var genes = JsonConvert.DeserializeObject<JsonWrapper>(json);https://stackoverflow.com/questions/62095516
复制相似问题