简体   繁体   English

如何使用C#反序列化JSON?

[英]How to deserialize JSON with C#?

I have the simple JSON: 我有简单的JSON:

[
  {
    "new_as_cod": "0010955",
    "as_nome": "NAME",
    "as_cpf": "1212121212",
    "as_email": "IM@UOL.COM.BR",
    "as_cep": "88.025-200",
    "igr_nome": "1\u00aa IGREJA BATISTA - FLORIANOPOLIS",
    "id": "2781",
    "valor": "50.00",
    "pg_tipo_id": "CC",
    "status": "Ativo",
    "idstatus": "1"
  }
]

... and a C# class generated from here : ...以及从此处生成的C#类:

 public class RootObject
    {
        public string new_as_cod { get; set; }
        public string as_nome { get; set; }
        public string as_cpf { get; set; }
        public string as_email { get; set; }
        public string as_cep { get; set; }
        public string igr_nome { get; set; }
        public string id { get; set; }
        public string valor { get; set; }
        public string pg_tipo_id { get; set; }
        public string status { get; set; }
        public string idstatus { get; set; }
    }

I have tried this: 我已经试过了:

RootObject data = JsonConvert.DeserializeObject<RootObject>(stringdate);

But I get the error: 但是我得到了错误:

错误

How can I solve it? 我该如何解决?

[{ "new_as_cod": "0010955", "as_nome": "NAME", "as_cpf": "1212121212", "as_email": "IM@UOL.COM.BR", "as_cep": "88.025-200", "igr_nome": "1\u00aa IGREJA BATISTA - FLORIANOPOLIS", "id": "2781", "valor": "50.00", "pg_tipo_id": "CC", "status": "Ativo", "idstatus": "1" }]

If it has [] this is a collection. 如果具有[]则为集合。

Try this. 尝试这个。

JsonConvert.DeserializeObject<List<RootObject>>(stringdate);

是的,此JSON是一个集合,因此该变量也需要列出。

List<RootObject> data = JsonConvert.DeserializeObject<List<RootObject>>(stringdate);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM