-1

I am getting response in the format of JObject which is like this.

        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"
         }
       }
     }

Now the "transaction_details" that value ("ae6df74457a7cbf62caf") rapidly change every transaction,So how i get that value and after that how I get "mihpayid" key value in C#.

halfer
  • 19,824
  • 17
  • 99
  • 186
amit kumar
  • 95
  • 1
  • 10
  • You question is not clear, please ask your question as [minimal, complete, verifiable question](https://stackoverflow.com/help/mcve). – Emre Savcı Nov 27 '18 at 05:12
  • I have response in the format of string and i convert it into JObject . Its complete man what do you want please tell me... – amit kumar Nov 27 '18 at 05:20

3 Answers3

2

You need to create a class to represent the JSON being returned. The transaction_details part of the object can be represented as a Dictionary for example:

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; }
}

And, instead of using JObject.Parse(...) use the JsonConvert.DeserializeObject to convert the JSON into your C# class:

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

You can now reference all the properties of your JSON using the myData object.

You can loop through the transaction_details reading each entry.

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

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

The values of the variables in the for loop are:

id = the unique ID for each transaction i.e. "ae6df74457a7cbf62caf"

transaction = a Transaction object with all the values from the unique transaction

mihpayid = the mihpayid within the unique transaction i.e. "403993715518647003"

Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • Hi, Buddy thanks a lot i get the value of every key which is in Transaction_Details.But, can u help me to get ( "ae6df74457a7cbf62caf") which is in Transaction_Details other wise all i get. – amit kumar Nov 27 '18 at 05:41
  • var myData = JsonConvert.DeserializeObject(response); if (myData.transaction_details.Count > 0) { string TDval = myData.transaction_details.Values.ToString(); } – amit kumar Nov 27 '18 at 05:42
  • I've updated my answer to include how to read the values from the deserialized JSON. I hope that makes it clearer for you :-) – Simply Ged Nov 27 '18 at 06:00
  • Yes Buddy Thanks a Lot for help, your solution is really great.But , little bit its long below your answer which is provided by -- Praneet Nadkar it's too small and also that is working. But really thanks a lot i use your solution in my code . – amit kumar Nov 27 '18 at 06:10
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]);
        }

OUTPUT:

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" }

  • Thanks Buddy Its so easy thanks a lot....Really your solution is very very small and Understandable for implement.Really Thanks a lot from core of my Heart. – amit kumar Nov 27 '18 at 06:19
0

You can use JObject and convert it to C# object. Something like this: How to deserialize an JObject to .NET object

Praneet Nadkar
  • 823
  • 7
  • 16