Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _03.JSON_Stringify
  8. {
  9.     public class Students
  10.     {
  11.         public Students(string name, string age, List<string> grades)
  12.         {
  13.             this.Name = name;
  14.             this.Age = age;
  15.             this.Grades = grades;
  16.         }
  17.         public override string ToString()
  18.         {
  19.             return $"{{name:\"{Name}\",age:{Age},grades:[{string.Join(", ", Grades)}]}}";
  20.         }
  21.  
  22.  
  23.  
  24.         public string Name { get; set; }
  25.  
  26.         public string Age { get; set; }
  27.  
  28.         public List<string> Grades { get; set; }
  29.     }
  30.  
  31.     class Program
  32.     {
  33.         static void Main(string[] args)
  34.         {
  35.             var input = Console.ReadLine()
  36.                 .Split(new string[] { " : " }, StringSplitOptions.RemoveEmptyEntries)
  37.                 .ToArray();
  38.  
  39.             List<Students> studentsData = new List<Students>();
  40.            
  41.  
  42.             while (input[0] != "stringify")
  43.             {
  44.                 string name = input[0];
  45.                 var tokens = input[1].Split(new [] {" -> "}, StringSplitOptions.RemoveEmptyEntries).ToArray();
  46.                 string age = tokens[0].Trim(new[] { '-', '>' })
  47.                     .Trim();
  48.                
  49.                 List<string> grades = new List<string>();
  50.                 if (tokens.Length > 1)
  51.                 {
  52.                      grades = tokens[1].Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  53.                    
  54.                 }
  55.                
  56.  
  57.          
  58.                
  59.                 var student = new Students(name, age, grades);
  60.                 studentsData.Add(student);
  61.  
  62.                 input = Console.ReadLine()
  63.                 .Split(new string[] { " : " }, StringSplitOptions.RemoveEmptyEntries)
  64.                 .ToArray();
  65.             }
  66.  
  67.  
  68.             Console.WriteLine("["+string.Join(",",studentsData)+"]");
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement