-6

I have json data like

{ "John": {"StudentName": "John anow", "RegistrationNo": "xxxxx", "Grade":"B"},
"Methwe 0": {"StudentName": "Methew", "RegistrationNo": "xxxxx", "Grade":"B"},
"Johnsan 09": {"StudentName": "Johnsan anow", "RegistrationNo": "xxxxx", "Grade":"B"}
 }

how to deserialize this using C# and convert to JSON Array.

M Imran
  • 109
  • 7

1 Answers1

1

Below shows you one approach to consider. The key is the use of the StudentDetails class to define the necessary properties of your individual students (as well as the Dictionary - so that it is keyed by the string (e.g. "John")).

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Sample
{
    public class StudentDetails
    {
        public string StudentName { get; set; }
        public string RegistrationNo { get; set; }
        public string Grade { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var input = @"{ ""John"": { ""StudentName"": ""John anow"", ""RegistrationNo"": ""xxxxx"", ""Grade"":""B""},
""Methwe 0"": { ""StudentName"": ""Methew"", ""RegistrationNo"": ""xxxxx"", ""Grade"":""B""},
""Johnsan 09"": { ""StudentName"": ""Johnsan anow"", ""RegistrationNo"": ""xxxxx"", ""Grade"":""B""}
        }";


            var output = JsonConvert.DeserializeObject<Dictionary<string, StudentDetails>>(input);

            Console.ReadLine();

        }
    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63