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?
3
Answers
It would be nice to know what the specific error is but from inspecting the JSON it looks like you are stepping into the wrong parent node.
Try
Also, make sure the array has at least 14 entries under the parent
data
.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
another way is to deserialize data to c# class
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 typeNewtonsoft.Json.Linq.JArray
.Then you try to call
JsonNode.Parse(JsonResponse);
which is not possible with the given argument, because the type ofJsonResponse
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.