0

I have a Json Response to deserialize, after doing the deserialize I get error.

This is the Json array I want to deserialize:

{
    "status": 200,
    "success": true,
    "message": "Success",
    "data": [
        {
            "transaction_reference": "REF202301031128311645_1",
            "virtual_account_number": "1234567890",
            "principal_amount": "200.00",
            "settled_amount": "199.80",
            "fee_charged": "0.20",
            "transaction_date": "2023-01-03T00:00:00.000Z",
            "transaction_indicator": "C",
            "remarks": "Transfer FROM Transfer | [1234567] TO Merchant Name",
            "currency": "NGN",
            "alerted_merchant": false,
            "merchant_settlement_date": "2023-01-03T10:29:25.847Z",
            "frozen_transaction": null,
            "customer": {
                "customer_identifier": "1234567"
            }
        }
    ]
}

From the Array, I want to get the following as variable: status, transaction_reference, Virtual_account_number, principal_amount, customer_identifier,

below is what I tried:

string data = response.Content;
string dataT = data.Replace("", string.Empty);
dynamic stuff = JObject.Parse(dataT);
dynamic status = stuff.status;

var JsonResponse = stuff.data;
var ResponseX = JsonNode.Parse(JsonResponse); // Deserializing json Array

dynamic transaction_reference = ResponseX[0]["transaction_reference"];
dynamic virtual_account_number = ResponseX[1]["virtual_account_number"];
dynamic principal_amount = ResponseX[2]["principal_amount"];
dynamic customer = ResponseX[13]["customer_identifier"];
dynamic customer_identifier = customer.customer_identifier;

The error I got is as below

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500 Message=The best overloaded method match for 'System.Text.Json.Nodes.JsonNode.Parse(ref System.Text.Json.Utf8JsonReader, System.Text.Json.Nodes.JsonNodeOptions?)' has some invalid arguments
Source= StackTrace:

The error occurred at the line

var ResponseX = JsonNode.Parse(JsonResponse); // Deserializing json Array

What I really want achieve is to separate the following and get each as variable:

status,

transaction_reference,

Virtual_account_number,

principal_amount,

customer_identifier,

Please can someone point to me where my error is?

4
  • 1
    What is the error you are seeing? Commented Jan 3, 2023 at 14:07
  • 2
    Consider removing the use of dynamic. Either deserialize to a strongly typed model, or use JSON with LINQ Commented Jan 3, 2023 at 14:09
  • Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a minimal reproducible example. Please edit your question to add these details into it or we may not be able to help. Commented Jan 3, 2023 at 14:10
  • Thank you for your response, I just edited for more clarity on my issue. Commented Jan 3, 2023 at 14:34

2 Answers 2

1

you have too much code that you don't need, if you don't want to deserialize to c# class, you can do it this way too

    var ResponseX = JsonNode.Parse(response.Content); // Parse the response string
    int status= (int)ResponseX["status"];

    var data=ResponseX["data"][0];
    
    string transaction_reference = (string) data["transaction_reference"];
    string virtual_account_number = (string) data["virtual_account_number"];
    string principal_amount = (string) data["principal_amount"];
    string customer = (string) data["customer_identifier"];
    int customer_identifier = Convert.ToInt32 ((string) data["customer"]["customer_identifier"]);

another way is to deserialize data to c# class

Datum d = System.Text.Json.JsonSerializer.Deserialize<Datum>(data.AsObject());
    
string transaction_reference = d.transaction_reference;
    
string virtual_account_number = d.virtual_account_number;

int customer_identifier = Convert.ToInt32( d.customer.customer_identifier);


public class Datum
{
    public string transaction_reference { get; set; }
    public string virtual_account_number { get; set; }
    public string principal_amount { get; set; }
    public string settled_amount { get; set; }
    public string fee_charged { get; set; }
    public DateTime transaction_date { get; set; }
    public string transaction_indicator { get; set; }
    public string remarks { get; set; }
    public string currency { get; set; }
    public bool alerted_merchant { get; set; }
    public DateTime merchant_settlement_date { get; set; }
    public object frozen_transaction { get; set; }
    public Customer customer { get; set; }
}

public class Customer
{
    public string customer_identifier { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This code replaces your solution and works like a charm, but keep in mind that your actual error was using the wrong types, which was not shown on compile type because of the usage of the dynamic type. @Emeka Okoye
0

You are passing an invalid into JsonNode.Parse(). The error is thrown at runtime because you use dynamic as type and the compiler does not know the type at compile time.

In line dynamic stuff = JObject.Parse(dataT); you are using Newtonsoft to parse the data. In Line var JsonResponse = stuff.data; you are writing a variable of type Newtonsoft.Json.Linq.JArray. Then you try to call JsonNode.Parse(JsonResponse); which is not possible with the given argument, because the type of JsonResponse is not valid.

You will get this error on compile time, when you use the "real" types instead of dynamic.

To fix your issue, you should use the same function for parsing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.