Emeka Okoye
Emeka Okoye

Reputation: 27

How to Deserialize Json Array

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?

Upvotes: 0

Views: 462

Answers (2)

Serge
Serge

Reputation: 43860

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

Upvotes: 1

RedFox
RedFox

Reputation: 455

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.

Upvotes: 0

Related Questions