反序列化JSON时出现ArgumentNullException

问题描述 投票:0回答:3

我正在尝试反序列化这样的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,因为反序列化后该对象为null,是否构建了错误的对象?起初,我坚信它可能与null值或json文件的生成顺序有关。我使用了NullValueHandling.Ignore,但这些值仍然为空,如何解决?抱歉,您无法登录,谢谢

我正在尝试反序列化json,例如{“ geneslist”:[{“ seqid”:“ NC_045512.2”,“ source”:“ RefSeq”,“ type”:“ region”,“ start”:“ 1“,” end“:” 29903“,” score“:”。“,” strand“:” +“,” ...

c# json serialization
3个回答
0
投票

GenesWrapper应该是这样的:


0
投票

这不起作用。您的json不适合创建C#类。属性“国家”在您的一个(第二个)json对象中出现两次。


0
投票

您可以改为这样做:

© www.soinside.com 2019 - 2024. All rights reserved.