0

Input string i have is:

{"data": "{
\"AckNo\":\"112110045659900\",
\"AckDt\":\"2021-01-24 01:14:00\",
\"Irn\":\"543e3dcd9ecd215ffdb75496a72d5272922b1a929f6c8f728298a9db910c8c5b\"}"}

I want read the data of AckNo, AckDt, Irn.

How to separate or split the above value part of the data node.

xanatos
  • 109,618
  • 12
  • 197
  • 280

3 Answers3

1

You have a JSON inside another JSON... Using Json.NET:

var des = (JObject)JsonConvert.DeserializeObject(str);

// The inner data
string data = (string)des["data"];

// From string to object
var desInner = (JObject)JsonConvert.DeserializeObject(data);

// The properties you want
string ackNo = (string)desInner["AckNo"];
DateTime ackDt = (DateTime)desInner["AckDt"];
string irn = (string)desInner["Irn"];

If ackDt is optional you could:

var ackDt = (DateTime?)desInner["AckDt"];

And if you want ackNo to be a number:

long ackNo = (long)desInner["AckNo"];

or (if it is optional):

long? ackNo = (long?)desInner["AckNo"];
xanatos
  • 109,618
  • 12
  • 197
  • 280
0

Please check the following thread. It would help you.

You just need to create class which could represent the json string and follow.

How to Convert JSON object to Custom C# object?

Nantharupan
  • 594
  • 3
  • 15
0

First, create a class structure that matches your json.

public class MyData
{
    public Details Data { get; set; }
}
public class Details
{
    public string AckNo { get; set; }
    public DateTime AckDt { get; set; }
    public string Irn { get; set; }
}

Then use JsonConvert to deserialize it. Install package NewtonSoft.Json to use JsonConvert.

var result = JsonConvert.DeserializeObject<MyData>(yourInputString);
var ackNo = result.Data.AckNo;
var ackDt = result.Data.AckDt;
var irn = result.Data.Irn;
Qudus
  • 1,440
  • 2
  • 13
  • 22