从C#中的JSON字符串中获取一些值?

时间:2016-03-06 17:26:08

标签: c# json

字符串来源

{
"Amount": 16700000,
"CardNumber": "0095",
"MerchantReference": "7654325",
"PaymentReference": "FBN|WEB|WEBP|3-02-2016|170619",
"RetrievalReferenceNumber": "000000088836",
"LeadBankCbnCode": null,
"LeadBankName": null,
"SplitAccounts": [],
"TransactionDate": "2016-02-03T16:41:43.923",
"ResponseCode": "00",
"ResponseDescription": "Approved Successful"
}

如何使用c sharp获取Transaction DateResponseDescriptionTransaction date的值,

我需要代码,谢谢

1 个答案:

答案 0 :(得分:1)

查看此库:https://www.nuget.org/packages/Newtonsoft.Json。 这是代码。首先,定义要放置值的对象。例如:

[Serializable]
public class TransactionResponse
{
    public DateTime TransactionDate { get; set; }
    public string ResponseCode { get; set; }
    public string ResponseDescription { get; set; }
}

然后,像这样使用你的课程:

using Newtonsoft.Json;

...

string jsonContent = @"{
""Amount"": 16700000,
""CardNumber"": ""0095"",
""MerchantReference"": ""7654325"",
""PaymentReference"": ""FBN|WEB|WEBP|3-02-2016|170619"",
""RetrievalReferenceNumber"": ""000000088836"",
""LeadBankCbnCode"": null,
""LeadBankName"": null,
""SplitAccounts"": [],
""TransactionDate"": ""2016-02-03T16:41:43.923"",
""ResponseCode"": ""00"",
""ResponseDescription"": ""Approved Successful""
}";

var response = JsonConvert.DeserializeObject<TransactionResponse>(jsonContent);