'How to convert an embedded JSON string to a JSON Object in C#
I have a function that returns data as a json string and I am calling that string inside my api but I want my api to return a json object instead of a json string.
This is what the function looks like:
public JObject InquiryPaymentAPI(string id)
{
string paymentInfo = PaymentInquiry.IPayment(id);
return JObject.Parse(paymentInfo);
}
This returns a json object but the embedded part looks like an empty array how do I get it to return all the data please help I am new to c#
{
"paymentId": [],
"paymentRequestId": [],
"paymentAmount": [
[
[]
],
[
[]
]
],
"paymentStatus": [],
"result": [
[
[]
],
[
[]
],
[
[]
]
]
}
Solution 1:[1]
If you know the json response, you can create a model class for it. On VS 2022 you can right click on editor and do paste special Edit --> Paste special --> Paste JSON as Classes. You should get something like below.
public class Model
{
public object[] paymentId { get; set; }
public object[] paymentRequestId { get; set; }
public object[][][] paymentAmount { get; set; }
public object[] paymentStatus { get; set; }
public object[][][] result { get; set; }
}
After using that Model class in your code, your code should look like below
using System.Text.Json;
public Model InquiryPaymentAPI(string id)
{
string paymentInfo = PaymentInquiry.IPayment(id);
return JsonSerializer.Deserialize<Model>(paymentInfo);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ragavendra |
