Json Parsing in c#(Console application)

Alpha Beta 41 Reputation points
2021-09-02T10:04:41.497+00:00

I need to parse Json into C# (Console Application) and also need to view the parsed data in a table format.

Json Code:

{
  "RLC": [
    {
      "PAR": ""
    },
    {
      "PAR": ""
    },
    {
      "PAR": ""
    }
  ],
  "PR":
    

Please help out.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,479 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 26,671 Reputation points
    2021-09-02T13:21:18.793+00:00

    You did not mention the target framework or share the expected table layout.

    Below is a .NET 5 example where the JSON data is stored in a file.

        class Program  
        {  
      
            static void Main(string[] args)  
            {  
                string json = File.ReadAllText(@"C:/temp/json.txt");  
                Rootobject root = JsonSerializer.Deserialize<Rootobject>(json);  
      
                Console.WriteLine(root.PNRAmount.AuthorizedBalanceDue);  
      
                foreach(var item in root.RecordLocator)  
                {  
                    Console.WriteLine($"\t{item.PNR}");  
                }    
            }  
        }  
        public class Rootobject  
        {  
            public Recordlocator[] RecordLocator { get; set; }  
            public Pnramount PNRAmount { get; set; }  
        }  
      
        public class Pnramount  
        {  
            public string BalanceDue { get; set; }  
            public string AuthorizedBalanceDue { get; set; }  
            public string SegmentCount { get; set; }  
            public string PassiveSegmentCount { get; set; }  
            public string TotalCost { get; set; }  
            public string PointsBalanceDue { get; set; }  
            public string TotalPointCost { get; set; }  
            public object[] AlternateCurrencyCode { get; set; }  
            public string AlternateCurrencyBalanceDue { get; set; }  
        }  
      
        public class Recordlocator  
        {  
            public string PNR { get; set; }  
        }  
    
    0 comments No comments