CSharp · Console
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters;
using System.IO;
using System.Data;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Reflection;
using System.Collections.ObjectModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using NUnit.Framework; // Throws NUnit.Framework.AssertionException
//https://stackoverflow.com/questions/59997900/how-to-efficiently-load-api-results-into-a-datatable
public static class JsonExtensions
{
public static T DeserializeAnonymousType<T>(TextReader textReader, T anonymousTypeObject, JsonSerializerSettings settings = null)
{
return (T)JsonSerializer.CreateDefault(settings).Deserialize(textReader, typeof(T));
}
}
class TestClass
{
public static void Test()
{
using (var streamReader = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(GetJson()))))
{
var root = JsonExtensions.DeserializeAnonymousType(streamReader,
new { success = default(bool), list = default(DataTable) });
if (root.success)
{
var dt = root.list;
// return the table
Console.WriteLine("Deserialized {0}:", dt.GetType());
Console.WriteLine(JsonConvert.SerializeObject(dt, Formatting.Indented));
}
else
{
// Handle failure
}
}
}
static string GetJson()
{
var json = @"{
""success"": true,
""code"": 0,
""msg"": ""성공하였습니다."",
""list"": [
{
""code"": ""ANT"",
""code_name"": ""소둔구분"",
""p_code"": """",
""p_code_name"": """",
""code_type"": 0,
""code_level"": 0,
""max_level"": 1,
""description"": ""GROP : 99 DETL : ANT SUBS : 00"",
""create_user"": ""SYSTEM"",
""create_time"": ""2019-04-24T17:58:58.000+0000"",
""disable_yn"": ""Y""
},
{
""code"": ""2ANT"",
""code_name"": ""소둔구분"",
""p_code"": """",
""p_code_name"": """",
""code_type"": 0,
""code_level"": 0,
""max_level"": 1,
""description"": ""GROP : 99 DETL : ANT SUBS : 00"",
""create_user"": ""SYSTEM"",
""create_time"": ""2019-04-24T17:58:58.000+0000"",
""disable_yn"": ""Y""
},
{
""code"": ""3ANT"",
""code_name"": ""소둔구분"",
""p_code"": """",
""p_code_name"": """",
""code_type"": 0,
""code_level"": 0,
""max_level"": 1,
""description"": ""GROP : 99 DETL : ANT SUBS : 00"",
""create_user"": ""SYSTEM"",
""create_time"": ""2019-04-24T17:58:58.000+0000"",
""disable_yn"": ""Y""
},
]
}
";
return json;
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Json.NET version: " + typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine();
try
{
TestClass.Test();
}
catch (Exception ex)
{
Console.WriteLine("Failed with unhandled exception: ");
Console.WriteLine(ex);
throw;
}
}
public static string GetNetCoreVersion()
{
//https://techblog.dorogin.com/how-to-detect-net-core-version-in-runtime-ecd65ad695be
//
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];
return null;
}
}