如何从C#中的json获取键名及其值,而其值也以一对键值形式存在

时间:2018-11-27 05:07:49

标签: c# json

我正在得到像这样的JObject格式的响应。

        JObject response = JObject.Parse(account);

       {
        "status": 1,
        "msg": "1 out of 1 Transactions Fetched Successfully",
        "transaction_details": {
           "ae6df74457a7cbf62caf": {
           "mihpayid": "403993715518647003",
           "request_id": "",
           "bank_ref_num": "201833147408756",
           "amt": "88.00",
           "transaction_amount": "88.00",
           "txnid": "ae6df74457a7cbf62caf",
           "additional_charges": "0.00",
           "Settled_At": "0000-00-00 00:00:00"
         }
       }
     }

现在,该值(“ ae6df74457a7cbf62caf”)的“ transaction_details”迅速更改了每笔交易,因此我如何获取该值,然后如何在C#中获取“ mihpayid”键值。

3 个答案:

答案 0 :(得分:2)

您需要创建一个类来表示要返回的JSON。 对象的transaction_details部分可以表示为Dictionary,例如:

class Account
{
    public int Status { get; set; }
    public string Msg { get; set; }

    public Dictionary<string, TransactionDetails> transaction_details { get; set; }
}

class TransactionDetails
{
    public string mihpayid { get; set; }
    public string requestid { get; set; }
    public string bank_ref_num { get; set; }
    public string amt { get; set; }
    public string transaction_amount { get; set; }
    public string txnid { get; set; }
    public string additional_charges { get; set; }
    public string Settled_At { get; set; }
}

而且,不要使用JObject.Parse(...)来使用JsonConvert.DeserializeObject来将JSON转换为C#类:

var myData = JsonConvert.DeserializeObject<Account>(account);

您现在可以使用myData对象引用JSON的所有属性。

您可以遍历transaction_details阅读每个条目。

foreach(var item in myData.transaction_details)
{
    var id = item.Key;

    var transaction = item.Value;
    var mihpayid = transaction.mihpayid;
}

for循环中变量的值是:

id =每个交易的唯一ID,即“ ae6df74457a7cbf62caf”

transaction =一个Transaction对象,其中包含唯一交易中的所有值

mihpayid =唯一交易中的mihpayid,即“ 403993715518647003”

答案 1 :(得分:1)

JObject response = JObject.Parse("{\r\n        \"status\": 1,\r\n        \"msg\": \"1 out of 1 Transactions Fetched Successfully\",\r\n        \"transaction_details\": {\r\n           \"ae6df74457a7cbf62caf\": {\r\n           \"mihpayid\": \"403993715518647003\",\r\n           \"request_id\": \"\",\r\n           \"bank_ref_num\": \"201833147408756\",\r\n           \"amt\": \"88.00\",\r\n           \"transaction_amount\": \"88.00\",\r\n           \"txnid\": \"ae6df74457a7cbf62caf\",\r\n           \"additional_charges\": \"0.00\",\r\n           \"Settled_At\": \"0000-00-00 00:00:00\"\r\n         }\r\n       }\r\n     }");     

        JObject transaction = JObject.Parse(response["transaction_details"].ToString());
        foreach(var token in transaction.Properties()) 
        {
            var rapidlyChangingId = token.Name;
            Console.WriteLine("Rapidly Changing ID: " + rapidlyChangingId);
            Console.WriteLine(transaction[rapidlyChangingId]);
        }
  

输出:

Rapidly Changing ID: ae6df74457a7cbf62caf { "mihpayid": "403993715518647003", "request_id": "", "bank_ref_num": "201833147408756", "amt": "88.00", "transaction_amount": "88.00", "txnid": "ae6df74457a7cbf62caf", "additional_charges": "0.00", "Settled_At": "0000-00-00 00:00:00" }

答案 2 :(得分:0)

您可以使用JObject并将其转换为C#对象。像这样: How to deserialize an JObject to .NET object