[Fork] [Fork] questions/48429702/c-parsing-a-non-json-api-response-to-a-class-object-with-x-properties

CSharp · Console

/Program.cs

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 Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;	

    // https://stackoverflow.com/questions/48429702/c-parsing-a-non-json-api-response-to-a-class-object-with-x-properties

    class TestClass
    {
        public static void Test()
        {
            var jsonString = GetJson();

            var arrays = JsonConvert.DeserializeObject<object [][]>(jsonString);
			
            var newJson1 = JsonConvert.SerializeObject(arrays, Formatting.Indented);

            Console.WriteLine("\nRe-serialized BinanceKlineData as an array of arrays: ");
            Console.WriteLine(newJson1);

			Console.WriteLine("\nDeserialized types:");			
			var types = arrays.Select(a => a.Select(i => i == null ? null : i.GetType().Name));
			Console.WriteLine(JsonConvert.SerializeObject(types, Formatting.Indented));
        }

        static string GetJson()
        {
            var json = @"[

[123,""0.01"",""0.02"",""0.03"",""0.04"",""12345.00000"",123456789,""300.000"",4000,""123.000"",""456.000"",""0""],
[456,""0.04"",""0.03"",""0.02"",""0.01"",""54321.00000"",987654321,""500.000"",4000,""123.000"",""456.000"",""1""],
[789,""0.05"",""0.06"",""0.07"",""0.08"",""12345.00000"",123456789,""700.000"",8000,""456.000"",""123.000"",""0""]

]";
            return json;
        }
    }

	public class Program
	{
		public static void Main()
		{
			Console.WriteLine("Environment version: " + Environment.Version);
			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 class AssertionFailedException : System.Exception
    {
        public AssertionFailedException() : base() { }

        public AssertionFailedException(string s) : base(s) { }
    }

    public static class Assert
    {
        public static void IsTrue(bool value)
        {
            IsTrue(value, "failed");
        }

        public static void IsTrue(bool value, string message)
        {
            if (value == false)
                throw new AssertionFailedException(message ?? "failed");
        }
    }
;