我在将json转换为object时遇到错误

时间:2018-06-07 12:02:27

标签: c# json

我正在尝试将JSON转换为对象以下是我的json

{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","event":"payment.captured","contains":["payment"],"payload":{"payment":{"entity":{"id":"pay_AKR45WLH0g1ANu","entity":"payment","amount":100,"currency":"INR","status":"captured","order_id":"order_AKR41LsWIgOAB1","invoice_id":null,"international":false,"method":"netbanking","amount_refunded":0,"refund_status":null,"captured":true,"description":"Admission Fees","card_id":null,"bank":"SBIN","wallet":null,"vpa":null,"email":"xxxxx.xxxx@xxx.xxx","contact":"xxxxx","notes":{"address":"NA","merchant_order_id":"2516"},"fee":2,"tax":0,"error_code":null,"error_description":null,"created_at":1528367383}}},"created_at":1528367384}

我尝试转换为对象的代码是

 jsonString = JsonConvert.SerializeObject(documentContents);  
 RazorPayPayload desJsonString = JsonConvert.DeserializeObject<RazorPayPayload>(jsonString);

以及我想要反序列化的类

 public class RazorPayPayload
{
    public string entity { get; set; }
    public string account_id { get; set; }
    public string events { get; set; }
    public List<string> contains { get; set; }
    public payments payload { get; set; }
    public string created_at { get; set; }
}
public class payments
{
    public Entities payment { get; set; }
}
public class notes
{
    public string address { get; set; }
    public string merchant_order_id { get; set; }
    public string source { get; set; }
}
public class Entities
{
    public Entity entity { get; set; }
}
public class Entity
{
    public string id { get; set; }
    public string entity { get; set; }
    public string amount { get; set; }
    public string currency { get; set; }
    public string order_id { get; set; }
    public string invoice_id { get; set; }
    public string international { get; set; }
    public string method { get; set; }
    public string amount_refunded { get; set; }
    public string refund_status { get; set; }
    public string captured { get; set; }
    public string description { get; set; }
    public string card_id { get; set; }
    public string bank { get; set; }
    public string wallet { get; set; }
    public string vpa { get; set; }
    public string email { get; set; }
    public string contact { get; set; }
    public notes notes { get; set; }
    public string fee { get; set; }
    public string tax { get; set; }
    public string error_code { get; set; }
    public string error_description { get; set; }
}

我收到错误“将值转换为'PayPayr_Razor_Webhook.RazorPayPayload'的错误”

2 个答案:

答案 0 :(得分:0)

您是否尝试过更改:

 {"entity":"event","account_id":"acc_8yTsyb2WJOlcka","event":

{"entity":"event","account_id":"acc_8yTsyb2WJOlcka","events":

这样可以更好地匹配您的班级定义。或者将您的课程字段重命名为 event

答案 1 :(得分:0)

有一个有用的visual studio函数,用于将json对象转换为c#类。

将JSON复制到剪贴板中。 (如果JSON对象属性包含数据,则有帮助,以确定数据类型)

转到visual studio并将光标放在您想要粘贴到c#类的位置。

点击edit =&gt; paste special =&gt;将json粘贴为类