Question40529125 how-to-deserialize-json-to-object-so-nested-json-will-convert-into-json-list-ins

CSharp · Console

/Program.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Collections.ObjectModel;
using System.Reflection;
using System.ComponentModel;
using System.Globalization;
using System.Collections.Specialized;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;


    public class RawConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            throw new NotImplementedException();
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
                return null;
            var raw = JRaw.Create(reader);
            return raw.ToString();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var s = (string)value;
            writer.WriteRawValue(s);
        }
    }

    public class Employee
    {
        public int EmpId { get; set; }
        public string EmpName { get; set; }
        // **Note** : I'm not using List<Address> data type for Address, instead of I want list of address in JSON string
        [JsonConverter(typeof(RawConverter))]
        public string Address { get; set; }
    }

    public class RootObject
    {
        public List<Employee> Employees { get; set; }
    }

    class TestClass
    {
        static string GetJson()
        {
            var s = @"{
   ""Employees"":[
      {
         ""EmpId"":1,
         ""EmpName"":""ABC"",
         ""Address"":[
            {
               ""AddressId"":1,
               ""Address"":""Something""
            },
            {
               ""AddressId"":2,
               ""Address"":""Anything""
            }
         ]
      },
      {
         ""EmpId"":2,
         ""EmpName"":""XYZ"",
         ""Address"":[
            {
               ""AddressId"":1,
               ""Address"":""Something""
            },
            {
               ""AddressId"":2,
               ""Address"":""Anything""
            }
         ]
      }
   ]
}";
            return s;
        }

        public static void Test()
        {
            var jsonEmployees = GetJson();

            var Employees = JsonConvert.DeserializeObject<RootObject>(jsonEmployees);

            Console.WriteLine("Addresses: ");
            Console.WriteLine(String.Join("\n", Employees.Employees.Select(e => e.Address)));

            Console.WriteLine("Re-serialized JSON: ");
            Console.WriteLine(JsonConvert.SerializeObject(Employees, Formatting.Indented));
        }
    }	
				
	public class Program
	{
		public static void Main()
		{
            Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
			
			TestClass.Test();
		}
	}
;