NinaSmith

Untitled

Apr 3rd, 2017
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. namespace _03.JSONparse
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     public 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.  
  16.     public class JSONparse
  17.     {
  18.         public static void Main()
  19.         {
  20.             var studentsData = new List<Student>();
  21.  
  22.             var input = Console.ReadLine().Split(new char[] {'}'},StringSplitOptions.RemoveEmptyEntries);
  23.  
  24.             for (int i = 0; i < input.Length - 1; i++)
  25.             {
  26.                 var currentStudent = input[i].Split(new char[] { '[', '}', ':', '"', ',', ' ', '{' , ']'}, StringSplitOptions.RemoveEmptyEntries);
  27.  
  28.                 var newStudent = new Student
  29.                 {
  30.                     Name = currentStudent[1],
  31.                     Age = int.Parse(currentStudent[3]),
  32.                     Grades = currentStudent.Skip(5).Select(int.Parse).ToList()
  33.                 };
  34.  
  35.                 studentsData.Add(newStudent);
  36.                
  37.             }
  38.  
  39.  
  40.  
  41.             foreach (var student in studentsData)
  42.             {
  43.                 if(student.Grades.Count > 0)
  44.                 {
  45.                     Console.WriteLine($"{student.Name} : {student.Age} -> {string.Join(", ", student.Grades)}");
  46.                 }
  47.                 else
  48.                 {
  49.                     Console.WriteLine($"{student.Name} : {student.Age} -> None");
  50.                 }
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment