Advertisement
YavorGrancharov

JSON_Stringify(text_processing)

Jul 30th, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace JSON_Stringify
  6. {
  7.     class Student
  8.     {
  9.         public string Name { get; set; }
  10.  
  11.         public int Age { get; set; }
  12.  
  13.         public List<int> Grades { get; set; }
  14.     }
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             string input = Console.ReadLine();
  20.  
  21.             List<Student> students = new List<Student>();
  22.  
  23.             string concat = string.Empty;
  24.             while (input != "stringify")
  25.             {
  26.                 string[] tokens = input
  27.                     .Split(new[] { ' ', ':', '-', '>', ','},
  28.                     StringSplitOptions.RemoveEmptyEntries);
  29.  
  30.                 string name = tokens[0];
  31.  
  32.                 int age = int.Parse(tokens[1]);
  33.  
  34.                 List<int> grades = tokens
  35.                     .Skip(2)
  36.                     .Select(int.Parse)
  37.                     .ToList();
  38.  
  39.                 Student newStudent = new Student
  40.                 {
  41.                     Name = name,
  42.                     Age = age,
  43.                     Grades = grades
  44.                 };
  45.  
  46.                 students.Add(newStudent);
  47.  
  48.                 input = Console.ReadLine();
  49.             }
  50.  
  51.             foreach (Student student in students)
  52.             {
  53.                 concat += (string.Format("{{name:\"{0}\",age:{1},grades:[{2}]}},",
  54.                     student.Name,
  55.                     student.Age,
  56.                     string.Join(", ", student.Grades)));
  57.             }
  58.             Console.WriteLine("[{0}]", concat.TrimEnd(','));
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement