Exception handling in Json Parsing

Alpha Beta 41 Reputation points
2021-09-03T04:43:04.563+00:00

I have parsed my json data to datatable and received output. But there is a loop which extends my elements number of times. I know that the keyword Distinct(); can be used but without using the keyword, how could it be solved.

My Code:

Please Help

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,648 questions
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 27,696 Reputation points
    2021-09-03T13:42:44.19+00:00

    This is a duplicate thread. The JSON data is not a table. The JSON data is a single object that contains two, maybe three, types; RecordLocator and Pnramount. Pnramount contains a collection of an unknown type; AlternateCurrencyCode.

    Like your previous thread I recommend deserializing the JSON stream into a standard C# type. Working with a C# type is an easier type safe way to manipulate data than messing around with a JObject and string indexes. You'll find a basic code sample, similar to your other thread, below. The code sample uses a type, Record, to store the RecordLocator and AlternateCurrencyCode arrays as a delimited string. Feel free to modify the code to suite your needs.

        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 string[] AlternateCurrencyCode { get; set; }  
            public string AlternateCurrencyBalanceDue { get; set; }  
        }  
      
        public class Recordlocator  
        {  
            public string PNR { get; set; }  
        }  
      
        public class Record   
        {  
            public string RecordLocator { get; set; }  
            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 string AlternateCurrencyCode { get; set; }  
            public string AlternateCurrencyBalanceDue { get; set; }  
        }  
      
        public class Program  
        {  
            static void Main(string[] args)  
            {  
                string json = File.ReadAllText(@"C:/temp/json.txt");  
                Rootobject root = JsonConvert.DeserializeObject<Rootobject>(json);  
      
                Record r = new Record()  
                {  
                    RecordLocator = string.Join(":", root.RecordLocator.Select(m => m.PNR)),  
                    AlternateCurrencyBalanceDue = root.PNRAmount.AlternateCurrencyBalanceDue,  
                    AlternateCurrencyCode = string.Join(", ", root.PNRAmount.AlternateCurrencyCode),  
                    AuthorizedBalanceDue = root.PNRAmount.AuthorizedBalanceDue,  
                    BalanceDue = root.PNRAmount.BalanceDue,  
                    PassiveSegmentCount = root.PNRAmount.PassiveSegmentCount,  
                    PointsBalanceDue = root.PNRAmount.PointsBalanceDue,  
                    SegmentCount = root.PNRAmount.SegmentCount,  
                    TotalCost = root.PNRAmount.TotalCost,  
                    TotalPointCost = root.PNRAmount.TotalPointCost  
                };  
      
                foreach (var prop in r.GetType().GetProperties())  
                {  
                    Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(r, null));  
                }  
            }  
        }  
    
    0 comments No comments